Preserving theme on TreeViewItem with ItemContainerStyle

WPF Studio, Themes, and Shared Library for WPF Forum

Posted 7 years ago by Kasper
Version: 11.2.0552
Avatar
Hi guys,

I have a TreeView where I set a Style for the TreeViewItem to make them use properties on the data objects for the Expanded and Selected state. Something like this:
<TreeView.ItemContainerStyle>
    <Style TargetType="{x:Type TreeViewItem}">
        <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
        <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
    </Style>
</TreeView.ItemContainerStyle>
Unfortunately, this seems to turn off the theme-stuff I get from you guys, making this particular TreeView look different than the other TreeView's in my Window. How can I prevent that from happening? I really like the theme you have added to my TreeView's and so on :)

Comments (2)

Posted 7 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Kasper,

That is correct, a control can only have one other style applied to other than it's default style. This includes implicit Styles or if the Style property is explicitly set, which ItemContainerStyle ultimately does. Our theming for the native controls uses implicit Styles, so setting ItemContainerStyle effectively short circuits that.

All of our styles are exposed using ComponentResourceKeys defined in the SharedResourceKeys class. So you can use something like:
xmlns:themes="http://schemas.actiprosoftware.com/winfx/xaml/themes"

<Style TargetType="{x:Type TreeViewItem}" BasedOn="{StaticResource {x:Static themes:SharedResourceKeys.TreeViewItemStyleKey}}">
    <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
    <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
</Style>


Actipro Software Support

Posted 7 years ago by Kasper
Avatar
Excellent, that was exactly what I was looking for! Thank you :)
The latest build of this product (v2019.1 build 0681) was released 1 month ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.