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

Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top