首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法在wpf中将treeviewitem转换为treeviewitem

无法在wpf中将treeviewitem转换为treeviewitem
EN

Stack Overflow用户
提问于 2010-05-28 21:02:30
回答 2查看 2.2K关注 0票数 1

我有webservice asmx,并且有一些类:

代码语言:javascript
复制
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:

代码语言:javascript
复制
Country[] items = new MyService().GetListOfCountries();
structureTree.ItemsSource = items;

myTree代码:

代码语言:javascript
复制
<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:

代码语言:javascript
复制
private void structureTree_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
TreeViewItem treeViewItem = structureTree.SelectedItem as TreeViewItem;
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-05-28 21:07:40

SelectedItem将实际包含国家、地区或地区(或null)。如果你真的想要TreeViewItem,你可以使用strutureTree.ItemContainerGenerator.ContainerFromItem(structureTree.SelectedItem)

票数 4
EN

Stack Overflow用户

发布于 2010-05-28 21:05:21

对,是这样。你应该期待一个国家/地区作为你的SelectedItem。WPF的工作方式与Windows窗体完全不同。这一切都与数据绑定有关!

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2929140

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档