0

i have an ExtendedTreeView control which extendes TreeView. My extendedTreeView has a property called Highlight so unlike a normal TreeView, i want the extendedTreeView to highlight items based on this Highlight property rather than IsSelected. So I have a style defined for a TreeView like below.

<Style x:Key="TreeViewStyle" TargetType="{x:Type TreeViewItem}">

        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TreeViewItem}">

                        <Border Name="Bd"
                              Background="Transparent"
                              //other stuff
                        </Border>

                    <ControlTemplate.Triggers>

                        <Trigger Property="IsSelected" Value="true">
                            <Setter TargetName="Bd" Property="Background" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Background}" />   
                        </Trigger> 
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

And then i define another style for the extendedTreeView which is based on the TreeViewStyle. The problem is that when i try to set "Bd" which is the border, it can't recognize it and has no idea what im referring to.

<Style x:Key="TreeViewStyle2" TargetType="{x:Type controls:ExtendedTreeView}" BasedOn="{StaticResource TreeViewStyle}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TreeViewItem}">
                    <ControlTemplate.Triggers>
                        <Trigger Property="controls:ExtendedTreeView.Highlight" Value="true">
                            <Setter TargetName="Bd" Property="Background" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Background}" /> 
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

IS there anyway of fixing this? or an alternative way of doing it? thanks

an007
  • 111
  • 1
  • 9
  • You misunderstanding what [BasedOn](https://learn.microsoft.com/en-us/dotnet/api/system.windows.style.basedon) does. Since you define new `Template` - the old `Template` value is lost, all its triggers, elements, names, etc. – Sinatr Apr 11 '19 at 11:36
  • If you want to keep template, then you can prevent old template triggers logic from working by setting value (in this case `Background`) using [higher precedence](https://docs.microsoft.com/en-us/dotnet/framework/wpf/advanced/dependency-property-value-precedence). You can achieve it by setting local value in code behind. See [this](https://stackoverflow.com/q/8126700/1997232), you have to find `Bd` in setter (callback if `Highlight` is dependency property) and set it to appropriate value. If values are defined in resources (with keys), then you can easily override them in xaml if needed. – Sinatr Apr 11 '19 at 11:51

0 Answers0