Set TreeViewItem visibility from DataTemplate RRS feed

  • Question

  • I would like to set the visibility of a TreeViewItem based on the properties of the bound data. It is easy to hide the data itself, but the child items remain visible because the DataTemplate is hidden - not the entire item.

    Any ideas?
    Tuesday, May 6, 2008 9:02 AM


  • You can use DataTrigger to implement this type of feature, here is a XAMLPad ready example:

    Code Snippet

    <Page xmlns=""
            <XmlDataProvider x:Key="tree" XPath="*">
                    <Products xmlns="">
                            <Name>Product 1</Name>
                            <Name>Product 2</Name>
            <HierarchicalDataTemplate ItemsSource="{Binding XPath=child::*}" x:Key="template">
                <TextBlock Name="textBlock" Text="{Binding Path=Name}"/>
            ItemsSource="{Binding Source={StaticResource tree}}"
            ItemTemplate="{StaticResource template}">
                <Style TargetType="{x:Type TreeViewItem}">
                    <Setter Property="IsExpanded" Value="True"/>
                        <DataTrigger Binding="{Binding Path=Name}" Value="Name">
                            <Setter Property="Visibility" Value="Collapsed"/>

    Hope this helps
    Thursday, May 8, 2008 8:49 AM