Defining default layout properties - wpf

I want to set consistent margins throughout all controls within an entire view. I currently use XAML:
<Window.Resources>
<Thickness x:Key="ConsistentMargins">0,10,0,0</Thickness>
</Window.Resources>
<!-- ... -->
<!-- ... -->
<!-- ... -->
<MyControl1 Margin="{StaticResource ConsistentMargins}">
<MyControl2 Margin="{StaticResource ConsistentMargins}">
<MyControl3 Margin="{StaticResource ConsistentMargins}">
Is there a way to set a default layout style for controls to avoid the above repeated code shown above?

You can create your own style with TargetType and this style will be assigned to all object of type which you specified in TargetType. But in this case your created style will be applied only for speciefied type of object, but not for derived type.
E.g. you can create style for all buttons like this:
<Style TargetType="{x:Type Button}">
<Setter Property="Margin" Value="0,10,0,0" />
</Style>
I think that this makes sense that style is not applied from base class, because I want say "My all buttons looks like...", but I want not say "Everything looks like...".

You may create a base default style, perhaps for FrameworkElement, and let the default styles for other element types extend the base style:
<Window.Resources>
<Style TargetType="FrameworkElement">
<Setter Property="Margin" Value="0,10,0,0"/>
</Style>
<Style TargetType="TextBlock" BasedOn="{StaticResource {x:Type FrameworkElement}}"/>
<Style TargetType="TextBox" BasedOn="{StaticResource {x:Type FrameworkElement}}"/>
<Style TargetType="Label" BasedOn="{StaticResource {x:Type FrameworkElement}}"/>
...
</Window.Resources>

Related

WPF Style BasedOn not working as expected

Why do my Radiobutton not look like a togglebutton?
Take a look at the code
<Style x:Key="ButtonBaseStyle" TargetType="{x:Type ButtonBase}">
<Setter Property="Height" Value="100" />
</Style>
<Style BasedOn="{StaticResource ButtonBaseStyle}" TargetType="{x:Type Button}" />
<Style BasedOn="{StaticResource ButtonBaseStyle}" TargetType="{x:Type ToggleButton}" />
<Style BasedOn="{StaticResource {x:Type ToggleButton}}" TargetType="{x:Type RadioButton}" />
<StackPanel>
<Button>Button</Button>
<ToggleButton>Toggle</ToggleButton>
<RadioButton>Radio</RadioButton>
</StackPanel>
If I remove the buttonbase style it works
The looks of your RadioButton is not as ToggleButton cause of you have now changed the style hierarchy of ButtonBase->ToggleButton->RadioButton (the order in which styles and templates are overwritten in derived classes of WPF).
Your new hierarchy of style gives the most priority to the style of ButtonBase. So you have a new ToggleButton style that is derived from a ButtonBase and then you overwrite the RadioButton style with that. So noone can tell you for sure which properties have you overwritten and which will be the final set of properties.
To understand this change the XAML as following:
<Style BasedOn="{StaticResource {x:Type ToggleButton}}" TargetType="{x:Type ToggleButton}" />
<Style BasedOn="{StaticResource {x:Type ToggleButton}}" TargetType="{x:Type RadioButton}" />
and now see the look & feel of your RadioButton.
I'm not clarifying what is exactly happened and why your ToggleButton is not affected so much.
But as I said it's all about the style hierarchy which is build for a control at last just before rendering on the UI.

How to extend instead of overriding WPF Styles

I want to use custom theme in my application and as far as I know I can accomplish this by using resource dictionary and referencing it in App.xaml. Styles would override the defaults like this:
<Style TargetType="{x:Type Label">
<Setter Property="Foreground" Value="Green" />
</Style>
Now as I guess the default Label style is overriden with same values but all my label fonts are green. The problem starts when I want to style one label somewhere again. When I want to change some other property in my Grid like this
<Grid.Resources>
<Style TargetType="{x:Type Label">
<Setter Property="FontSize" Value="28" />
</Style>
</Grid.Resources>
All labels inside my grid are losing their foreground color and have default one again (didn't I override defaults in previous step?). After some tries I found out that to do this properly i have to add another property to Style declaration BasedOn={StaticResource {x:Type Label}}" and it works. This is kind of weird for me because now I will have to repeat same BasedOn code in whole app and this is not how styling works - this should be done automatically! For example in HTML + CSS styles are inherited and merged and in WPF they are replaced...
Notice that when I don't use any styles controls still get their look from somehwere (System Themes?). How can I tell them to look for defaults somewhere else so without any additional code on styles they will think that they should be green by default?
Is there any way I can automate setting BasedOn property? Or maybe there is a better to do this overally?
I had the same problem. I used Zack's answer and improved it like following so if you don't specify a style the overridden default is still taken in account. It's basically what you would have done but just once in the ResourceDictionary.
<Window x:Class="TestWpf.RandomStuffWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Random Stuff Window">
<Window.Resources>
<ResourceDictionary>
<!-- Default Label style definition -->
<Style TargetType="{x:Type Label}">
<Setter Property="Foreground" Value="Green" />
</Style>
<!-- Extending default style -->
<Style TargetType="{x:Type Label}"
x:Key="LargeGreenForegroundLabel"
BasedOn="{StaticResource {x:Type Label}}">
<Setter Property="FontSize" Value="28" />
</Style>
</ResourceDictionary>
</Window.Resources>
<StackPanel>
<Button Click="Button_Click">Click</Button>
<Label Content="GreenForegroundLabel" /> <!-- Uses default style -->
<Label Style="{StaticResource LargeGreenForegroundLabel}"
Content="LargeGreenForegroundLabel" />
</StackPanel>
</Window>
Wpf has different levels of styles, that are applied in order of global > local. A style set directly on a control will override a style set globally, like in your example. I was trying to find a list of all the different places that a control looks for its styles but I cannot find one at the moment. As far as I know, you will have to use the BasedOn property to inherit a style and not completely override the properties of that style with the style you set locally.
Here is an example of a resource dictionary that has styles based on another style, so that you don't have do repeat the BasedOn binding over and over, you can just set the style on the specific element you want to have that style.
<Window x:Class="TestWpf.RandomStuffWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Random Stuff Window">
<Window.Resources>
<ResourceDictionary>
<Style TargetType="{x:Type Label}"
x:Key="GreenForegroundLabel">
<Setter Property="Foreground" Value="Green" />
</Style>
<Style TargetType="{x:Type Label}"
x:Key="LargeGreenForegroundLabel"
BasedOn="{StaticResource GreenForegroundLabel}">
<Setter Property="FontSize" Value="28" />
</Style>
</ResourceDictionary>
</Window.Resources>
<StackPanel>
<Button Click="Button_Click">Click</Button>
<Label Style="{StaticResource GreenForegroundLabel}"
Content="GreenForegroundLabel" />
<Label Style="{StaticResource LargeGreenForegroundLabel}"
Content="LargeGreenForegroundLabel" />
</StackPanel>
</Window>

In WPF, Is there any way to combine two Style for one control?

My situation is like following.
I have a App.xaml which includes Style for ListView like this:
<Style x:Key="{x:Type ListViewItem}" TargetType="ListViewItem">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
...
However, I wanna add some styles into another xaml, let's say in Window.xaml like this:
<ListView AlternationCount="2" Background="#FFECECEC">
<ListView.Resources>
<Style x:Key="{x:Type ListViewItem}" TargetType="{x:Type ListViewItem}">
<EventSetter Event="PreviewMouseDoubleClick" Handler="OnPreviewMouseDoubleClick" />
</Style>
</ListView.Resources>
</ListView>
So, what I want to do is define style for base design in App.xaml as Default style.
Then, add some modify such as adding a context menu, adding events from each xaml.
But, with above implementation, Style defined in App.xaml will be overwrote by Style defined in Window.xaml.
Is there any way to solve the issue and achieve it?
Styles have a BasedOn property:
<Style x:Key="Style1">
...
</Style>
<Style x:Key="Style2" BasedOn="{StaticResource Style1}">
...
</Style>
Btw: <Style x:Key="{x:Type ListViewItem}" seems a bit weird. The x:Key should be a unique key in a xaml dictionary - usually a string.
If the BasedOn attribute fits your needs, it's the best choice. For more complicated scenarios though, something like the solution referred to in this question is more flexible.

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

Styling an attached property with inheritance

My question is similar to this: WPF Style with no target type? but the problem is that mine is an attached property:
<Style TargetType="Shape" >
<Setter Property="z:Zommable.Unscale" Value="StrokeThickness" />
</Style>
My goal is: all object deriving from shape must have the attached property Zommable.Unscale with a value, but I can't find how to do it.
Thanks !
there is no difference to the other question. you will have to do exactly the same. So for each class deriving from Shape you will have to use a Style with that targettype that's BasedOn your style with your attached property.
So it's gonna be:
<Style x:Key="basicStyle">
<Setter Property="z:Zommable.Unscale" Value="StrokeThickness" />
</Style>
<Style TargetType="{x:Type Ellipse}" BasedOn="{StaticResource basicStyle}">
<!-- ... -->
</Style>
<Style TargetType="{x:Type Rectangle}" BasedOn="{StaticResource basicStyle}">
<!-- ... -->
</Style>
<!-- ... -->

Resources