开发者

WPF- Is there a way to stop TreeViewItems from becoming selected and activated when their parent TreeViewItem is selected?

开发者 https://www.devze.com 2023-02-02 23:58 出处:网络
I have a control template for TreeViewI开发者_JS百科tems and instead of showing the normal FocusVisualStyle I have a MultiTrigger set up like this:

I have a control template for TreeViewI开发者_JS百科tems and instead of showing the normal FocusVisualStyle I have a MultiTrigger set up like this:

<MultiTrigger>
    <MultiTrigger.Conditions>
        <Condition Property="IsSelected" Value="true"/>
        <Condition Property="IsSelectionActive" Value="true"/>
    </MultiTrigger.Conditions>
    <Setter Property="FontWeight" Value="Bold"/>
</MultiTrigger>

However this also causes the FontWeight to change to bold when a TreeViewItem's parent item is selected. Is there any way I can stop that from happening?


Great question. It has to do with dependency property value precedence.

This is happening because the child tree view items do not override the FontWeight property in any way so they are inheriting it from their visual parent. What you could do is add another normal Trigger for when IsSelected is false.

<Trigger Property="IsSelected" Value="false">
    <Setter Property="FontWeight" Value="Normal" />
</Trigger>
<MultiTrigger>
    <MultiTrigger.Conditions>
        <Condition Property="IsSelected" Value="true"/>
        <Condition Property="IsSelectionActive" Value="true"/>
    </MultiTrigger.Conditions>
    <Setter Property="FontWeight" Value="Bold"/>
</MultiTrigger>

Now the child TreeViewItem will have its FontWeight property set by a trigger that will override the inherited property from its selected parent.

0

精彩评论

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

关注公众号