【发布时间】:2010-10-20 06:02:46
【问题描述】:
为这篇长文道歉 -- 阅读了一些关于此的主题,但仍然难以实施。基本上,我有一个对象集合,定义为:
public class LibData
{
public string Name { get; set; }
ObservableCollection<LibObject> _list;
public ObservableCollection<LibObject> List { get { return _list; } }
public LibData(string name, LibDataType type)
{
this.Name = name;
_list = new ObservableCollection<LibObject>();
}
}
和对象:
public class LibObject
{
public string Name { get; set; }
public LibObject(string name)
{
this.Name = name;
}
}
我的主要问题是在 XAML 中,以及这个 TreeView 的样式。我需要为“根”项目设置特定样式,并为“叶子”设置特定样式。问题是,绑定列表中的一项是“Root->Leaf”,另一项是“Root->Child->Leaf”。 我试过这个:
<TreeView x:Name="myTree" ItemsSource="{x:Static local:myDataList}">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Path=List}" >
<Grid>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Name}" />
<CheckBox IsChecked="True" Content="HeaderCheckbox"/>
</StackPanel>
</Grid>
<HierarchicalDataTemplate.ItemTemplate >
<DataTemplate>
<Grid>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="True" Content="LeafCheckbox" />
<TextBlock Text="{Binding Path=Name}"/>
</StackPanel>
</Grid>
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
这显然适用于“Root->Leaf”项目,但不适用于“Root-Child-Leaf”。 XAML 实现似乎更像是一种“硬编码”解决方案,我知道项目布局始终是“根-> 叶”——如何使这个动态化? 同样,我已经看到了不同级别的解决方案(包括使用转换器),但我遇到的问题是我需要特定的样式来根和叶,而两者之间的级别不需要任何东西。 我想知道我是否完全看错了......
【问题讨论】: