开发者

HierarchicalDataTemplate TreeviewItem

开发者 https://www.devze.com 2023-01-16 18:22 出处:网络
I have the foll开发者_如何学Cowing xaml: <TreeView x:Name=\"tvCategoryList\" Grid.Column=\"0\" Padding=\"0\" ItemsSource=\"{Binding CategoriesList}\">

I have the foll开发者_如何学Cowing xaml:

<TreeView x:Name="tvCategoryList" Grid.Column="0" Padding="0" ItemsSource="{Binding CategoriesList}">
  <TreeView.ItemTemplate>
    <HierarchicalDataTemplate ItemsSource="{Binding Items}">
      <TextBlock Text="{Binding ItemName}"/>
    </HierarchicalDataTemplate>
  </TreeView.ItemTemplate>
</TreeView>

In the above code i'm binding ObservableCollection CategoriesList where the class CustomTreeItem has a Visibility property. How could i change the above code to bind the Visibility property so that it gets updated everytime (set to either visibile or collapsed) an item gets selected / de-selected?


You would use a style setter to manipulate the visibility of the item.

You use a binding that digs up to the TreeViewItem's selected property:

<HierarchicalDataTemplate ItemsSource="{Binding Items}">
  <TextBlock Text="{Binding ItemName}">
     <TextBlock.Style>
        <Style>
           <Style.Triggers>
              <DataTrigger Binding="{Binding Path=IsSelected, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TreeViewItem}}}" 
                  Value="True">
                  <Setter Property="TextBlock.Visibility" Value="false" />
              </DataTrigger>
           </Style.Triggers>
        </Style>
     </TextBlock.Style>
  </TextBlock>
</HierarchicalDataTemplate>

But this doesn't make it any less counter-intuitive.

0

精彩评论

暂无评论...
验证码 换一张
取 消