Can't cast treeviewitem as treeviewitem in wpf

Posted by phenevo on Stack Overflow See other posts from Stack Overflow or by phenevo
Published on 2010-05-28T13:02:30Z Indexed on 2010/05/28 13:11 UTC
Read the original article Hit count: 629

Filed under:
|
|

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;
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about wpf