在数据绑定的WPF树视图叶中开发复选框
我有下一个WPF树视图:在数据绑定的WPF树视图叶中开发复选框,wpf,data-binding,treeview,checkbox,hierarchicaldatatemplate,Wpf,Data Binding,Treeview,Checkbox,Hierarchicaldatatemplate,我有下一个WPF树视图: <TreeView HorizontalAlignment="Left" Margin="6,0,0,32" Name="tvProductos" Width="158"> <TreeViewItem Header="Securities" IsExpanded="True" FontWeight="Bold"> <TreeViewItem Header="Countr
<TreeView HorizontalAlignment="Left" Margin="6,0,0,32" Name="tvProductos" Width="158">
<TreeViewItem Header="Securities" IsExpanded="True" FontWeight="Bold">
<TreeViewItem Header="Country" Name="Country" FontWeight="Normal" />
<TreeViewItem Header="Currency" Name="Currency" FontWeight="Normal" />
<TreeViewItem Header="Type" Name="Type" FontWeight="Normal" />
<TreeViewItem Header="ISIN" Name="ISIN" FontWeight="Normal" />
<TreeViewItem Header="Description" Name="Description" FontWeight="Normal" />
</TreeViewItem>
<TreeViewItem Header="Issuer" IsExpanded="True" FontWeight="Bold">
<TreeViewItem Header="Name" Name="IssuerName" FontWeight="Normal" />
<TreeViewItem Header="Type" Name="IssuerType" FontWeight="Normal" />
<TreeViewItem Header="Market" Name="IssuerMarket" FontWeight="Normal" />
</TreeViewItem>
</TreeView>
问题是我需要在每个节点上添加一个复选框(低级和高级)。我一直在寻找解决方案,最好的是使用HierarchycalDataTemplate。但我从未发现一个同时具有固定节点和动态节点的示例。我试了几个例子,但都没能解决
你能帮我吗
先谢谢你
致以问候。我这样做的方式是使用一个helper类来表示可以检查的项目(有些人称之为“视图模型”): 这允许您为此特定类型指定
DataTemplate
:
<TreeView HorizontalAlignment="Left" Margin="6,0,0,32" Name="tvProductos" Width="158">
<TreeView.Resources>
<DataTemplate DataType="{x:Type local:SelectableItem}" xmlns:local="clr-namespace:WpfApplication1">
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding IsSelected}" />
<TextBlock Text="{Binding Label}" />
</StackPanel>
</DataTemplate>
</TreeView.Resources>
<TreeViewItem Header="Securities" IsExpanded="True" FontWeight="Bold">
<TreeViewItem Header="Country" Name="Country" FontWeight="Normal" />
<TreeViewItem Header="Currency" Name="Currency" FontWeight="Normal" />
<TreeViewItem Header="Type" Name="Type" FontWeight="Normal" />
<TreeViewItem Header="ISIN" Name="ISIN" FontWeight="Normal" />
<TreeViewItem Header="Description" Name="Description" FontWeight="Normal" />
</TreeViewItem>
<TreeViewItem Header="Issuer" IsExpanded="True" FontWeight="Bold">
<TreeViewItem Header="Name" Name="IssuerName" FontWeight="Normal" />
<TreeViewItem Header="Type" Name="IssuerType" FontWeight="Normal" />
<TreeViewItem Header="Market" Name="IssuerMarket" FontWeight="Normal" />
</TreeViewItem>
</TreeView>
这就应该起作用了。好的,它起作用了!非常感谢你。现在,如何检索所选节点?根据应用程序的体系结构,有大约1000种方法可以检索,其中一种方法是使用以下内容:
Country.ItemsSource.Cast().Where(I=>I.IsSelected)
;)完美地非常感谢你!
public class SelectableItem
{
public bool IsSelected { get; set; }
public string Label { get; set; }
}
<TreeView HorizontalAlignment="Left" Margin="6,0,0,32" Name="tvProductos" Width="158">
<TreeView.Resources>
<DataTemplate DataType="{x:Type local:SelectableItem}" xmlns:local="clr-namespace:WpfApplication1">
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding IsSelected}" />
<TextBlock Text="{Binding Label}" />
</StackPanel>
</DataTemplate>
</TreeView.Resources>
<TreeViewItem Header="Securities" IsExpanded="True" FontWeight="Bold">
<TreeViewItem Header="Country" Name="Country" FontWeight="Normal" />
<TreeViewItem Header="Currency" Name="Currency" FontWeight="Normal" />
<TreeViewItem Header="Type" Name="Type" FontWeight="Normal" />
<TreeViewItem Header="ISIN" Name="ISIN" FontWeight="Normal" />
<TreeViewItem Header="Description" Name="Description" FontWeight="Normal" />
</TreeViewItem>
<TreeViewItem Header="Issuer" IsExpanded="True" FontWeight="Bold">
<TreeViewItem Header="Name" Name="IssuerName" FontWeight="Normal" />
<TreeViewItem Header="Type" Name="IssuerType" FontWeight="Normal" />
<TreeViewItem Header="Market" Name="IssuerMarket" FontWeight="Normal" />
</TreeViewItem>
</TreeView>
Country.ItemsSource = (from d in db.PAISES
join p in db.PRODUCTOS on d.IDPAIS equals p.IDPAIS
select new SelectableItem { Label = d.NOMBREPAIS }).Distinct();