0

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.

DotNetter
  • 426
  • 2
  • 6
  • 19

1 Answers1

2

You can just Base it on whatever existing style you want and only modify the stuff necessary for the instance like you're doing using the Style BasedOn Property like;

<Style TargetType="TreeViewItem" 
       BasedOn="{StaticResource YourExistingStyleYouWantToBaseItOn}">
    <Setter Property="Background" Value="Red"/>
</Style>

Hope this Helps.

Chris W.
  • 22,835
  • 3
  • 60
  • 94
  • I had some thoughts about that decision. But is there way to import styles from DictionarySource which was defined in `Themes/generic.xaml` for custom control in separated CustomControls.dll – DotNetter Jul 24 '13 at 20:39
  • Not sure I understand the question, you mean like using MergedDictionaries? – Chris W. Jul 24 '13 at 20:42
  • Or do you mean something like this? http://stackoverflow.com/questions/338056/resourcedictionary-in-a-separate-assembly – Chris W. Jul 24 '13 at 20:47
  • Thank. I'll try your suggestion in a while and give feedback after that. – DotNetter Jul 25 '13 at 11:46