I developed custom TreeView
(call it MyTree
). In ResourceDictionary
in General.xaml
of this custom control I defined style for TreeViewItem
where set control template, that I needed to display every item. First I create special ControlTemplate:
<ControlTemplate TargetType="{x:Type TreeViewItem}" x:Key="MyTreeViewItem">
.........
</ControlTemplate>
Then I use it in custom style for MyTree
:
<Style TargetType="{x:Type local:MyTree}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:MyTree}">
<ControlTemplate.Resources>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="Template" Value="{StaticResource MyTreeViewItem}" />
</Style>
</ControlTemplate.Resources>
..........
<ItemsPresenter />
..........
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Everything works, while i use my control like:
<local:MyTree>
<local:MyTree.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding DependentSuites}"/>
</local:MyTree.ItemTemplate>
..........other property specific for MyTree control.............
</local:MyTree>
enter code here
And it does wrong when I try to add style for my TreeViewItem. For example, following code
<local:MyTree>
<local:MyTree.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding DependentSuites}"/>
</local:MyTree.ItemTemplate>
..........other property specific for MyTree control.............
<local:MultiColumnTreeView.Resources>
<Style TargetType="TreeViewItem"
<Setter Property="Background" Value="Red"/>
</Style>
</local:MultiColumnTreeView.Resources>
</local:MyTree>
cause wpf resets my custom template and start using default style for TreeViewItem. The same situation takes a place if I set any value for ItemsContainerStyle. Other words, any style modification rewrite my custom style in General.xaml. Why does it happen, and how to avoid that, combining all styles.