WPF Styles and Polymorphism - wpf

I know I'm a pervert, but I am very curious, is there a way to make custom controls to seek for it's base class style first, and then it's own.
Why I'm asking: I have some TreeView derived controls with custom item templates. I apply those templates, then I have a base style. Later I might apply some color palette. At the last step I have a problem. I need to apply 2 styles. PVStructuralTree is derived from TreeView it has some DependencyProperty DataTemplates that get inserted into resources in code.
PVStructuralTreeView
EmploeeTemplate
... more templates
Default style for PVStructuralTreeView:
<Style x:Key="DefaultPVStructuralTreeView" TargetType="{x:Type c:PVStructuralTreeView}" BasedOn="{StaticResource DefaultTreeView}">
<Setter Property="EmploeeTemplate"><!-- This get inserted inro Resources in code -->
<Setter.Value>
<DataTemplate DataType="{x:Type s:Emploee}">
...
</DataTemplate>
</Setter.Value>
</Setter>
... Lots of them here
</Style>
<Style TargetType="{x:Type c:PVStructuralTreeView}" BasedOn="{StaticResource DefaultPVStructuralTreeView}"/>
Default style for a TreeView (it's pretty big, so I won't post it here):
<Style TargetType="{x:Type TreeView}" BasedOn="{StaticResource DefaultTreeView}"/>
In color template.xaml file I'd like to have this + some magic to apply both styles at the same time (from Generic.xaml and themed one):
<Style x:Key="ThemedTreeView" TargetType="{x:Type TreeView}" BasedOn="{StaticResource DefaultTreeView}">
...
</Style>
<Style TargetType="{x:Type c:PVStructuralTreeView}" BasedOn="{StaticResource ThemedTreeView}"/>
But it just overwrites generic.xaml styles. I want it to add to it.
Now I'm doing this way:
<Style x:Key="ThemedPVStructuralTreeView" TargetType="{x:Type c:PVStructuralTreeView}" BasedOn="{StaticResource DefaultPVStructuralTreeView}">
... CopyPaste from ThemedTreeView ...
</Style>
<Style TargetType="{x:Type c:PVStructuralTreeView}" BasedOn="{StaticResource ThemedPVStructuralTreeView}"/>
Does anyone knows the way how to reuse the ThemedTreeView style here?

You can base a Style on Another (one only!) Style using the BasedOn property and override specific properties, but you cannot base a DataTemplate or a ControlTemplate on another template. This is not supported. A template must be defined as a whole:
WPF: Is there a way to override part of a ControlTemplate without redefining the whole style?

Related

How to use "BasedOn" attribute for ListView WPF controls?

In a UserControl XAML file, I want to define a ListView based on the predefined style. So I write the following lines:
<ListView Grid.Column="2" SelectionMode="Single">
<ListView.ItemContainerStyle>
<Style
TargetType="{x:Type ListViewItem}"
BasedOn="{StaticResource {x:Type ListViewItem}}">
Though, for the BasedOn attribute, I get the error: The resource "{x:Type ListViewItem}" could not be resolved..
If I replace the ListViewItem word with ListBoxItem or TreeViewItem the compilation is successful, but at runtime it raises a System.InvalidOperationException exception, specifying that the BasedOn attribute needs a ListBoxItem.
If I remove that attribute, it works, but it has a weird look.
If you are using MahApps.Metro, you should base your Style on the MetroListViewItem resource:
<Style TargetType="{x:Type ListViewItem}"
BasedOn="{StaticResource MetroListViewItem}">
...
BasedOn is used for Style inheritance, so it should be pointing to another ListViewItem Style that you've previously defined, not a type.
<Style TargetType="{x:Type ListViewItem}"
BasedOn="{StaticResource MyListViewItemBaseStyle}">

How do you declare styles in a way where the global style can be overriden?

I've got the following scenario
custom control library
<Style TargetType="{x:Type local:CustomControl}" x:Key="Default"> ... </Style>
<Style TargetType="{x:Type local:CustomControl}" BasedOn="{StaticResource Default}"> ... </Style>
application which references custom control library
<Style TargetType="{x:Type local:CustomControl}" BasedOn="{StaticResource Default}"> ... </Style>
for some reason the styles i am specifying in my app.xaml seem to have no effect. Could it be that the App.xaml is loaded prior to foreign generic.xaml contents? And if so - How does one fix that so global style overrides do work.

Override item Style Template with a DataTemplateSelector

I've got a Style on ListViewItem that sets the Theme property
<Style x:Key="{x:Type ListViewItem}" TargetType="{x:Type ListViewItem}">
<Setter Property="Template" Value="{StaticResource ListViewItemTemplate}"/>
</Style>
However, in one of the cases where I'm using a ListView I want to use a DataTemplateSelector to determine which Template to use
<Style x:Key="MyListStyle" TargetType="{x:Type ListView}" BasedOn="{StaticResource {x:Type ListView}}">
<Setter Property="ItemTemplateSelector" Value="{StaticResource MyItemTemplateSelector}"/>
</Style>
It's applied like this
<ListView Style="{StaticResource MyListStyle}/>
However, it would appear that the Style on the item takes over and that style is applied to all the items in the ListView. I found this question which had a similar problem, however, the solution simply doesn't use the Template on the Item style at all. I need to keep that.
I've played around with the ContentTemplateSelector by restyling the ListViewItems in that ListView
<Style x:Key="MyItemStyle" BasedOn="{StaticResource {x:Type ListViewItem}}" TargetType="{x:Type ListViewItem}">
<Setter Property="ContentTemplateSelector" Value="{StaticResource MyItemTemplateSelector}"/>
</Style>
However, the Template on the other style is used instead. If I try nulling the Template then nothing shows up at all!
<Setter Property="Template" Value="{x:Null}"/
Is there a way that I can replace the Template on the ListViewItems for a given ListView while keeping the rest of my Style?
I was unable to override the Template set on the ListViewItem in the ListView Style.
To get around this I replaced the Template on the ListViewItem with another ItemTemplateSelector in my base ListView Style. This selector always returns my original Template. In my ListView child Style the new ItemTemplateSelector is used.

Cannot base style on another style

I have some locally defined styles within Window.Resources. I have some styles for a TextBlock, TextBox, CheckBox and RadioButton. These are supposed to be applied to all controls in the window, so I haven't provided a value for x:Key. I would like them to inherit from a style targeting FrameworkElement. So I have something like:
<Style TargetType="{x:Type RadioButton}">
...
</Style>
<Style TargetType="{x:Type TextBlock}">
...
</Style>
<Style TargetType="{x:Type TextBox}">
...
</Style>
<Style TargetType="{x:Type CheckBox}">
...
</Style>
<Style x:Key="TriggerBase" TargetType="{x:Type FrameworkElement}">
<Style.Triggers>
<Trigger Property="UIElement.IsMouseOver" Value="True">
...
</Trigger>
</Style.Triggers>
</Style>
My problem is that I am unable to set the BasedOn property to inherit from my TriggerBase style. After looking at similar questions, such as this and this, I still cannot get it working. These answers suggest you need to specify the TargetType on your base style, which I have done.
I thought maybe the Styles have to target the exact same type, but after digging around on MSDN I found that wasn't the problem:
If you create a style with a TargetType property and base it on another style that also defines a TargetType property, the target type of the derived style must be the same as or be derived from the type of the base style.
If I set BasedOn like BasedOn="{DynamicResource TriggerBase}", it can find my TriggerBase, but I get an error stating:
A 'DynamicResourceExtension' cannot be set on the 'BasedOn' property
of type 'Style'. A 'DynamicResourceExtension' can only be set on a
DependencyProperty of a DependencyObject.
If I try BasedOn="{StaticResource TriggerBase}", I get an error that it cannot find TriggerBase. One of the linked answers above showed using StaticResource like BasedOn="{StaticResource {x:Type FrameworkElement}, but it still cannot resolve the style.
How can I inherit from the TriggerBase style? I'm targeting .NET 4.5.
You are correct and you can base your styles on FrameworkElement style just need to move
<Style x:Key="TriggerBase" TargetType="{x:Type FrameworkElement}">
</Style>
to the top and then
<Style TargetType="{x:Type RadioButton}" BasedOn="{StaticResource TriggerBase}">
will work

Why does x:Key unapply my TabItem Style

I am trying to make a custom style for a TabItem Header. I got it to work by accident.
this fails:
<Style TargetType="{x:Type TabItem}" x:Name="TabHeader3" x:Key="test">
but this works
<Style TargetType="{x:Type TabItem}" x:Name="TabHeader3">
What's going on?
The first Style you have defined is an "explicit" Style, so you must explicitly use it like so:
<TabItem Style="{StaticResource test}" />
The second Style you have defined is an "implicit" Style. So it will be applied to all TabItem controls below it in the visual/logical tree, or to all TabItem controls if it's defined in the application resources.
Your second Style is equivalent to:
<Style TargetType="{x:Type TabItem}" x:Name="TabHeader3" x:Key="{x:Type TabItem}">
So the key is the Type of the object to which it should be applied.
If a TabItem has a Style explicitly defined (like I show above), then any implicit Styles will not be used. Also, if you have two implicit Styles defined, then the closest one wins. So here:
<Window>
<Window.Resources>
<Style TargetType="{x:Type TabItem}">
<Setter Property="Background" Value="Red" />
</Style>
</Window.Resources>
<Grid>
<Grid.Resources>
<Style TargetType="{x:Type TabItem}">
<Setter Property="Background" Value="Blue" />
</Style>
</Grid.Resources>
...
<TabItem ... />
...
</Grid>
</Window>
The Blue Style will take precedence over the Red Style.
Finally, you generally don't need to include x:Name on your Styles.
If you add the style to a resource dictionary without a key then the style gets applied to all TabItems that are within the scope of the resource dictionary by default. If you add a Key to the style then you need to manually set the Style

Resources