2012-07-31 129 views
0

有沒有辦法讓Treeview中的父節點也有一些區別,以便用戶知道它是父母所在的位置?這可能是突出顯示單元格或更改文本,但我希望能夠單擊文件夾內的項目,但父節點不同,因此用戶知道他們在內部點擊了什麼內容。突出顯示TreeView中的父項目

謝謝。

回答

2

模型級別 - 在模型類中有「父」屬性。

E.g.如果您有遞歸綁定到一個類TreeViewMyItemClass

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標誌,然後將適當的樣式應用於該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