无法在wpf中将treeviewitem转换为treeviewitem

时间:2010-05-28 13:02:30

标签: c# wpf casting

我有web服务asmx,还有类:

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

我将它绑定到mz TreeView WPF:

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

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>

这给了我null:

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

2 个答案:

答案 0 :(得分:3)

SelectedItem实际上将包含Country,Area或Region(或null)。如果您真的想要TreeViewItem,可以strutureTree.ItemContainerGenerator.ContainerFromItem(structureTree.SelectedItem)

答案 1 :(得分:2)

正确。您应该期望Country作为您的SelectedItem。 WPF的工作方式与Windows Forms完全不同。这都是关于数据绑定的!