2009-09-21 3 views
1

TreeView이 있는데 여기에 HierarchicalDataTemplate을 사용합니다. 이것으로, 나는 다른 아이템들 (모두 같은 타입)을 채색한다.WPF 트리보기에서 트리거가있는 항목 숨기기

페이지에서 CheckBox을 클릭하면 일부 속성 (특정 속성 포함)을 숨기고 싶습니다. 많은 코드를 테스트했지만 아무 것도 제대로 작동하지 않습니다.

public partial class Window1 : Window 
{ 
    public Window1() 
    { 
     InitializeComponent(); 

     TreeElements tRoot = new TreeElements("Root", false); 
     TreeElements t1 = new TreeElements("Node 1", false); 
     TreeElements t11 = new TreeElements("Node 1-1", true); 
     t1.Children.Add(t11); 
     TreeElements t12 = new TreeElements("Node 1-2", false); 
     t1.Children.Add(t12); 
     tRoot.Children.Add(t1); 
     TreeElements t2 = new TreeElements("Node 2", false); 
     TreeElements t21= new TreeElements("Node 2-1", false); 
     TreeElements t211 = new TreeElements("Node 2-1-1", false); 
     t21.Children.Add(t211); 
     t2.Children.Add(t21); 
     tRoot.Children.Add(t2); 
     trv.Items.Add(tRoot); 
    } 
} 

public class TreeElements 
{ 
    public string Description { get; set; } 
    public List<TreeElements> Children { get; set; } 

    public TreeElements(string description, bool error) 
    { 
     Description = description; 
     _error = error; 
     Children = new List<TreeElements>(); 
    } 

    private bool _error; 

    public bool Error 
    { 
     get 
     { 
      bool bValue = _error; 
      foreach (TreeElements child in Children) 
       bValue = bValue || child.Error; 
      return bValue; 
     } 
    } 
} 

그리고 XAML :

<Window x:Class="WpfApplication2.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="Window1" Height="300" Width="300"> 

    <Window.Resources> 
     <HierarchicalDataTemplate x:Key="HDT_items" ItemsSource="{Binding Path=Children}"> 
      <TextBlock Text="{Binding Path=Description}" x:Name="txt" /> 
      <HierarchicalDataTemplate.Triggers> 
       <DataTrigger Binding="{Binding Path=Error}" Value="true"> 
        <Setter TargetName="txt" Property="Foreground" Value="Red" /> 
       </DataTrigger> 
      </HierarchicalDataTemplate.Triggers> 
     </HierarchicalDataTemplate>  
    </Window.Resources> 

    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition Height="*" /> 
     </Grid.RowDefinitions> 

     <CheckBox x:Name="chk">Mask some items</CheckBox> 

     <TreeView Grid.Row="1" x:Name="trv" ItemTemplate="{StaticResource HDT_items}" /> 
    </Grid> 
</Window> 

확인란을 선택하면 내가 원하는 표시하지 않도록 내가 여기

이 내 코드의 샘플입니다 ... 답을 찾고 있어요 오류 = false이지만 데이터 소스를 변경하지 않은 노드 그것은 가능한가, 그리고 어떻게?

답변

0

List에서 ObservableCollecton으로 TreeElement.Children 유형을 변경하십시오. 보기에 항목을 숨기는 대신 ViewModel의 기본 컬렉션에서 tme를 제거하면됩니다.

+0

네, 감사합니다. 기본 컬렉션을 다시 작성하는 함수와 함께 작동합니다. – Gulix