gpt4 book ai didi

c# - 无法在 wpf 中将 treeviewitem 转换为 treeviewitem

转载 作者:太空宇宙 更新时间:2023-11-03 20:40:38 25 4
gpt4 key购买 nike

我有 webservice 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;}

我将它绑定(bind)到 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>

这给了我空值:

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

最佳答案

SelectedItem 实际上将包含国家、地区或地区(或空值)。如果您真的想要 TreeViewItem,可以执行 structureTree.ItemContainerGenerator.ContainerFromItem(structureTree.SelectedItem)

关于c# - 无法在 wpf 中将 treeviewitem 转换为 treeviewitem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2929140/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com