PoshCode Archive  Artifact [273415fb55]

Artifact 273415fb55b34c17a265684571a6ecd155981399e3761d6cc82e3f0830d2e1d1:

  • File Boots-Hierarchical-Bind.ps1 — part of check-in [8d5239849c] at 2018-06-10 13:09:12 on branch trunk — Example of how to use Treeview Hierarchical databind using Boots. It took me a while to figure this out. Thanks JasonMArcher for your help! (user: unknown size: 2897)

# encoding: ascii
# api: powershell
# title: Boots Hierarchical Bind
# description: Example of how to use Treeview Hierarchical databind using Boots. It took me a while to figure this out. Thanks JasonMArcher for your help!
# version: 0.1
# type: module
# license: CC0
# x-poshcode-id: 2371
# x-archived: 2010-11-23T15:14:27
#
# This example will require the current changeset of PowerBoots (http://powerboots.codeplex.com/SourceControl/list/changesets)
#
######## CSV DATA #############
# Save the following data to a csv.


City,Team
"Los Angeles","Lakers"
"Los Angeles","Clippers"
"New York","Knicks"
"New York","Liberty"
"Sacramento","Kings"
 
######## CODE #################
 
$teams = Import-Csv "C:\testdata.csv"


[array]$cities = $teams | %{$_.City} | Sort -Unique


[Object[]]$test = foreach ($city in $cities){
        New-Object psobject -Property @{
                City = $city
                Teams = @(foreach($team in $($teams | ?{$_.City -eq $city} | %{$_.Team} | Sort -Unique)){
                        New-Object psobject -Property @{
                        Team = $Team
                        IsSelected = "True"
                        }
                })
        }
}
 
Import-Module PowerBoots

# Xaml layout -- this is where binding is set.
$xaml = @"
<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Name="Window">
        <Grid>
                <TreeView Name="treeview1">
                        <TreeView.ItemTemplate>
                                <HierarchicalDataTemplate ItemsSource="{Binding Teams}">
                                        <TextBlock Foreground="Green" Text="{Binding City}" />
                                        <HierarchicalDataTemplate.ItemTemplate>
                                                <DataTemplate>
                                                        <StackPanel Orientation="Horizontal">
                                                                <TextBlock Text="{Binding Team}" />
                                                                <CheckBox IsChecked="{Binding IsSelected}" IsEnabled="False"/>
                                                        </StackPanel>
                                                </DataTemplate>
                                        </HierarchicalDataTemplate.ItemTemplate>
                                  
                                </HierarchicalDataTemplate>
                        </TreeView.ItemTemplate>
                </TreeView>
        </Grid>
</Window>
"@
 
$MainWindow= New-BootsWindow -Title "Treeview Binding" -Width 100 -Height 150 -SourceTemplate $xaml -Async -Passthru -On_Loaded {
        Export-NamedElement
        $treeView1.ItemsSource = $test
}