Search code examples
.netwpfstylesresourcedictionaryitemcontainerstyle

WPF change ItemContainerStyle but keep a referenced base style


I have some styles in ResourceDictionaries from a 3-rd party dll (MaterialDesign)

 <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
        <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
        <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Dark.xaml" />

I have a TreeView which get's cool animation effect thanks to these styles being added in my UserControl's resource dictionary.

Later on I plan to implement some way so the user could switch between the three styles from the three dictionaries. Recommendations how how to best implement that are welcome.

I have a TreeView which I need to change the behavior of, such that some Items (or Nodes might be more correct) should have their children always expanded based on a Binding to a VM property. Also when I select an item in the tree view the item's child tree should automatically expand. When I leave the item the childtree should collapse unless it is set to be always expanded. I have managed to do all this by Edit a copy of the TreeView's ItemContainerStyle.

The problem and my question, as soon as I define/edit my own copy of the ItemContainerStyle I lose all the animation effects since it overrides the default set style from the MaterialDesign Resource(s). How can I make sure my ItemContainerStyle keeps the animations and styles also from the MaterialDesign Referenced ResourceDictionary(ies)?


Solution

  • The key for a default style is it's type. You can therefore use basedon with such a key.

         <TreeView.ItemContainerStyle>
             <Style TargetType="{x:Type TreeViewItem}"  BasedOn="{StaticResource {x:Type TreeViewItem}}"  >