开发者

Expand Child When Parent IsExpanded in a TreeView

I am attempting to expand a Child Node when its parent is expanded.

Otherwise stated: (Child.IsExpanded == Parent.IsExpanded)

This appears right, but does not seem to work:

<TreeView ItemsSource="{Binding}">
    <TreeView.ItemContainerStyle>
        <Style TargetType="TreeViewItem">
            <Style.Triggers>
                <DataTrigger Value="True" 
                             Binding="{Binding Path=IsExpanded, 
                             RelativeSource={RelativeSource 
                             Mode=FindAncestor, 
                             AncestorType={x:Type TreeViewItem}, 
                             AncestorLevel=2}}">
                    <Setter Property="IsExpanded" Va开发者_运维知识库lue="True" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TreeView.ItemContainerStyle>
</TreeView>

Neither does this:

<TreeView ItemsSource="{Binding}">
    <TreeView.ItemContainerStyle>
        <Style TargetType="TreeViewItem">
            <Setter Property="IsExpanded"
                    Value="{Binding Path=IsExpanded, 
                             RelativeSource={RelativeSource 
                             Mode=FindAncestor, 
                             AncestorType={x:Type TreeViewItem}, 
                             AncestorLevel=2}}" />
        </Style>
    </TreeView.ItemContainerStyle>
</TreeView>

What's missing here?

Thanks in advance.


Both work for me. If you tested it with static TreeViewItems make sure to apply the style via resources, the ItemContainerStyle is only relevant for dynamically created containers. Also note that user-interaction may set a local value, overriding those styles.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜