tags:

views:

44

answers:

2

Hi,

I've got webservice asmx, and there are classes:

Country

public string Name {get;set;}
public string Code {get;set;}
public List<Area> Areas {get;set;}

Area

public string Name {get;set;}
public string Code {get;set;}
public List<Regions> Provinces {get;set;}

Provinces

public string Name {get;set;}
public string Code {get;set;}

I bind it to mz TreeView WPF:

Country[] items = new MyService().GetListOfCountries();
structureTree.ItemsSource = items;

Code of myTree:

<UserControl x:Class="ObjectsAndZonesSimpleTree"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        <Grid>
        <StackPanel Name="stackPanel1">

            <GroupBox Header="Choose" Height="354" Name="groupBox1" Width="Auto">

                <TreeView Name="structureTree" SelectedItemChanged="structureTree_SelectedItemChanged" Grid.Row="0" Grid.Column="0" ItemsSource="{Binding}" Height="334" ScrollViewer.VerticalScrollBarVisibility="Visible" ScrollViewer.HorizontalScrollBarVisibility="Visible" Width="Auto" PreviewMouseRightButtonUp="structureTree_PreviewMouseRightButtonUp" FontFamily="Verdana" FontSize="12" BorderThickness="1" MinHeight="0" Padding="1" Cursor="Hand" Margin="-1">
                    <TreeView.Resources>
                        <HierarchicalDataTemplate DataType="{x:Type MyService:Country}" 
                                  ItemsSource="{Binding Path=ListOfRegions}">
                            <StackPanel Orientation="Horizontal">
                                <TextBlock TextAlignment="Justify" VerticalAlignment="Center" Text="{Binding Path=Name}"/>
                           </StackPanel>
                        </HierarchicalDataTemplate>
                        <HierarchicalDataTemplate DataType="{x:Type MyService:Region}" 
                                  ItemsSource="{Binding Path=Provinces}">
                            <StackPanel Orientation="Horizontal">
                                <TextBlock TextAlignment="Justify" VerticalAlignment="Center" Text="{Binding Path=Name}"/>


                </StackPanel>
                        </HierarchicalDataTemplate>
                        <DataTemplate DataType="{x:Type MyService:Province}" 
                                  ItemsSource="{Binding Path=ListOfCities}">
                            <StackPanel Orientation="Horizontal">
                                <TextBlock TextAlignment="Justify" VerticalAlignment="Center" Text="{Binding Path=Name}"/>
                </StackPanel>
                        </DataTemplate>

                    </TreeView.Resources>
                </TreeView>
            </GroupBox>

        </StackPanel>
    </Grid>

</UserControl>

This gives me null:

private void structureTree_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
TreeViewItem treeViewItem = structureTree.SelectedItem as TreeViewItem;
}
+2  A: 

Correct. You should expect a Country as your SelectedItem. WPF works entirely different than Windows Forms did. It's all about databinding!

Dave Markle
So, I can't get parent node, or do multiselecting ? :/
You can. But I don't remember that part. It's definitely a pain.
Dave Markle
+2  A: 

SelectedItem will actually contain a Country, Area, or Region (or null). If you really want the TreeViewItem, you can do strutureTree.ItemContainerGenerator.ContainerFromItem(structureTree.SelectedItem).

JustABill
I get error: An object reference is required for the non-static field, method, or property 'System.Windows.Controls.ItemContainerGenerator.ContainerFromItem(object)'
Can you post the exact code you used for this? It sounds like you tried to do `TreeView.ItemContainerGenerator`, but you have to access it from that specific tree with `structureTree.ItemContainerGenerator`.
JustABill