我正在使用HierarchicalDataTemplate将我的分层数据添加到Menu-Control.
<HierarchicalDataTemplate DataType="{x:Type local:MyType}" ItemsSource="{Binding Path=SubItems}">
<StackPanel>
<TextBlock Text="{Binding Name}"/>
</StackPanel>
</HierarchicalDataTemplate>
Run Code Online (Sandbox Code Playgroud)
我的菜单是这样创建的
<Menu>
<MenuItem ItemsSource="{Binding MyCollection}" Header="MainItem"></MenuItem>
</Menu>
Run Code Online (Sandbox Code Playgroud)
例如,如何为这些生成的MenuItem添加样式以设置IsCheckable属性.重要的是主MenuItem(此处名为"MainItem"的标题)不应用此样式,因此它不可检查.
我尝试了几种方法<Style>,<DataTemplate但没有成功.
考虑以下集合.
我希望以结构化的方式显示它,比如说TreeView.我希望能够围绕整个团体等绘制边框.
如何使用尽可能少的程序代码完成此操作?
我正在尝试将复选框添加到 WPF 中 TreeView 中的叶节点。如果我们在层次结构中有固定数量的级别并为每个级别使用 HierarchicalDataTemplate,我知道如何执行此操作。但是当我想要这个时该怎么做:
-Node 1
-- Node 1a (leaf node with checkbox)
-- Node 1b
--- Node 1bI (leaf node with checkbox)
-Node 2
-- Node 2a (leaf node with checkbox)
我将代码文件中的 DataContext 设置为 DataTable。只有一张桌子,与它本身有关。
DataContext = ds.MyDataTable;
Run Code Online (Sandbox Code Playgroud)
XAML:
<UserControl x:Class="JostyWpfControls.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="240" Width="312">
<UserControl.Resources>
<HierarchicalDataTemplate x:Key="myTemplate"
ItemsSource="{Binding myDatasetRelation}">
<CheckBox IsChecked="{Binding IsChosen}">
<TextBlock Text="{Binding Description}"/>
</CheckBox>
</HierarchicalDataTemplate>
</UserControl.Resources>
<Grid>
<TreeView x:Name="treeView"
ItemsSource="{Binding}"
ItemTemplate="{StaticResource myTemplate}">
</TreeView>
</Grid>
</UserControl>
Run Code Online (Sandbox Code Playgroud)
这是有效的,但给了我一个所有节点的检查箱。我只希望叶节点有一个复选框。
我有一个使用HierarchicalDataTemplate创建的树视图,您可以在下面的代码中看到:
<TreeView ItemsSource="{Binding AllFolders}" SelectedItemChanged="TreeView_SelectedItemChanged"/>
Run Code Online (Sandbox Code Playgroud)
和
<HierarchicalDataTemplate DataType="{x:Type model:Folder}" ItemsSource="{Binding Tools}">
<StackPanel Orientation="Horizontal">
<Image Source="{StaticResource ResourceKey=icon}"/>
<TextBlock Text="{Binding Title}"/>
</StackPanel>
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type model:Tool}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}"></TextBlock>
</StackPanel>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
它运行良好,但现在我需要在文件夹中添加其他类型的对象,以使树视图像这样:
工具和位置必须使用不同的DataTemplate.它们都是IList包含在Folder Class(IList<Tool>和IList<Location>)中.
它甚至可以做到吗?我怎样才能做到这一点?
谢谢你的帮助
关于HierarchicalDataTemplate似乎有大量的信息,但我很难找到足够的信息来帮助我解决包含不同类型的层次结构.
假设以下类结构:
public class classA
{
public string name{get;set;}
}
public class classB
{
public string name{get;set;}
public List<classA> subItems{get;set;}
}
public class classC
{
public string name{get;set;}
public List<classB> subItems{get;set;}
}
Run Code Online (Sandbox Code Playgroud)
现在假设类不是自引用的原因因此在我的层次结构中保持一种类型是包含它们的属性存在根本差异,有没有办法创建类型敏感的HierarchicalDataTemplate?