0

有没有办法让 Treeview 中的父节点也有一些区别,以便用户知道他们所在的父节点?可能是突出显示单元格或更改文本,但我希望能够单击文件夹内的项目,但父节点不同,因此用户知道他们在其中单击了什么。

谢谢你。

4

1 回答 1

2

模型级别 - 在您的模型类中具有“父”属性。

例如,如果你有一个递归绑定到TreeViewsay的类MyItemClass

public class MyItemClass
{
    public string MyHeader { get; set; } //Header text of each tree view item.
    public MyItemClass Parent { get; set; } //Parent MyItemClass object.
    public List<MyItemClass> Children { get; set; } //Children MyItemClass
    public bool IsSelected { get; set; } //When tree view item is clicked & selected.
    public bool IsHighlighted { get; set; } //When parent is highlighted.
}

所以当孩子被选中时......

private bool _isSelected;
public bool IsSelected 
{
  get { retrn _isSelected; }
  set
  {
     _isSelected = value;

     //The line below highlights the parent when either of the child is selected.
     this.Parent.IsHighlighted 
         = this.Parent.Children.Where(item => item.IsSelected).Any();

     //Notify Property Changed here...
   } 
} 

因此,正如父母强调...

private bool _isHighlighted;
public bool IsHighlighted
{
  get { retrn _isHighlighted; }
  set
  {
     _isHighlighted = value;

     //When a node is highlighted all its Parent nodes should be too.
     this.Parent.IsHighlighted 
         = this.Parent.Children.Where(item => item.IsHighlighted).Any();

     //Notify Property Changed here...
  } 
} 

然后,您可以IsHighlighted从树视图项的 undelrying 数据上下文中使用此标志,然后对其应用适当的样式TreeViewItem

    <TreeView ItemsSource="{Binding MyItems}">
        <TreeView.ItemTemplate>
            <HierarchicalDataTemplate ItemsSource="{Binding Children}">
                <TextBlock Text="{Binding MyHeader}">
                    <TextBlock.Style>
                        <Style TargetType="{x:Type TextBlock}">
                             <Style.Triggers>
                                <DataTrigger Binding="{Binding IsHighlighted}"
                                             Value="True">
                                    <Setter Property="FontWeight" Value="Bold"/>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </TextBlock.Style>
                </TextBlock>
            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>
    </TreeView>

确保INotifyPropertyChanged在项目类中的所有属性上实现,否则这些效果将不起作用。

于 2012-07-31T12:15:50.667 回答