HierarchicalDataTemplate TreeviewItem
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.
精彩评论