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

StackOverflow https://stackoverflow.com/questions/4612142

문제

I have a control template for TreeViewItems 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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top