tags:

views:

2041

answers:

2

I have a treeview in wpf. I want a different color when i select the treeviewitem.

Any Suggestions Plz..

Thanks

A: 

In the TreeView's ItemContainerStyle, define a trigger on the IsSelected property that will set the background color to whatever you want :

...
<TreeView.ItemContainerStyle>
    <Style TargetType="TreeViewItem">
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Background" Value="Yellow"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</TreeView.ItemContainerStyle>
...
Thomas Levesque
I used the above code for changing the color of selecteditem. but iam not getting the result. can u plz post full source code. Thanks
I don't have the full source code, I wrote that from scratch. But indeed I just tested it, and it doesn't work, probably because something in the default template overrides the TreeViewItem's background... So you will probably have to write your own template for TreeViewItems
Thomas Levesque
Thanks for the reply. now i got the solution for that. Simply i added solidcolorbrush for that.
A: 

Try following code. It should work.

<Style TargetType="{x:Type TreeViewItem}">
    <Style.Triggers>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Background" Value="Yellow"/>
        </Trigger>
    </Style.Triggers>
</Style>
hpatel