Button Resources defined in Style - wpf

I can create a button with rounded corners like this (from How to create/make rounded corner buttons in WPF?):
<Button>
<Button.Resources>
<Style TargetType="Border">
<Setter Property="CornerRadius" Value="5"/>
</Style>
</Button.Resources>
</Button>
How can I define this inside a style and then apply this style for each button ? I tried creating a style like this, but the CornerRadius is not available :
<Style x:Key = "myButtonStyle" TargetType = "Button">
<Setter Property = "Height" Value = "30"/>
<Setter Property = "Width" Value = "80"/>
<Setter Property = "Margin" Value = "10"/>
</Style>
Like this Pavel ?
<Style x:Key="s2" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="Background" Value = "Red" />
<Setter Property="CornerRadius" Value="5"/>
</Style>
WPF won't let me

You could define an implicit button style with a nested Border style:
<Style TargetType="Button">
<Style.Resources>
<Style TargetType="Border">
<Setter Property="CornerRadius" Value="5"/>
</Style>
</Style.Resources>
<Setter Property="Height" Value="30"/>
<Setter Property="Width" Value="80"/>
<Setter Property="Margin" Value="10"/>
</Style>

Related

Why is styles not getting applied to ToggleButton?

<Window.Resources>
<vm:NotesViewModel x:Key="vm"/>
<Style TargetType="ToggleButton">
<Setter Property="Height" Value="50"/>
<Setter Property="Width" Value="50"/>
</Style>
</Window.Resources>
But the ToggleButton in toolbars size remains unchanged. If set the size right there with defintion, it changes but not through window styles in resources.
What am I missing?
The ToolBar applies its own ToggleButton style which you can override by defining a style with an x:Key of ToolBar.ToggleButtonStyleKey:
<Style TargetType="ToggleButton">
<Setter Property="Height" Value="50"/>
<Setter Property="Width" Value="50"/>
</Style>
<Style x:Key="{x:Static ToolBar.ToggleButtonStyleKey}"
TargetType="ToggleButton" BasedOn="{StaticResource {x:Type ToggleButton}}"/>

Can I override/replace a property of a child-item in a default style

The default wpf MenuItem (on Menu) is constructed out of controls approx. like this:
grid; outer-rectangle; bg-rectangle; inner-rectangle; dockpanel; popup.
The dockpanel in turn consists of:
contentpresenter[icon]; path; contentpresenter[text]
The contentpresenter[text] consists of a TextBlock control.
What I want to achieve is to define a Style, as simple as possible, to change the VerticalAlignment property of this TextBlock, but only for the TextBlock in MenuItem, not in general.
<Style x:Key ="TextBlockCenterStyle" TargetType="{x:Type TextBlock}">
<Setter Property="VerticalAlignment" Value="Center" />
</Style>
<Style TargetType="MenuItem">
<Setter Property="FontSize" Value="11" />
<Setter Property="ItemContainerStyle" Value="TextBlockCenterStyle" />
<Style.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="VerticalAlignment" Value="Center" />
</Style>
</Style.Resources>
</Style>
I tried Style.Resources and ItemContainerStyle.
Cannot get it to work. ItemContainerStyle throws TargetInvocationException (from NullReferenceException) at run-time.
When it is possible it should be a general solution, something like FindChildControl?!
Did you try ItemContainerStyle?
Something like:
<MenuItem ItemContainerStyle = {StaticResource MyItemContainerStyle}../>
Then the MyItemContainerStyle have your
<Style x:Key ="MyItemContainerStyle" TargetType="{x:Type TextBlock}">
<Setter Property="VerticalAlignment" Value="Center" />
</Style>
=============== answer after EDIT ======================
try this:
<Style TargetType="MenuItem">
<Setter Property="FontSize" Value="11" />
<Setter Property="ItemContainerStyle" Value="{StaticResource TextBlockCenterStyle}" />
<Style.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="VerticalAlignment" Value="Center" />
</Style>
</Style.Resources>
</Style>

Overriding DataPointStyle in a WPF Toolkit Chart

I'd like to override the DataPointStyle of the LineSeries in my WPF Toolkit Chart:
<chart:LineSeries>
<chart:DataPointSeries.DataPointStyle>
<Style
BasedOn="{StaticResource {x:Type chart:LineDataPoint}}"
TargetType="{x:Type chart:LineDataPoint}">
<Setter Property="Width" Value="20" />
<Setter Property="Height" Value="20" />
</Style>
</chart:DataPointSeries.DataPointStyle>
</chart:LineSeries>
However when I do this I lose the automatic palette coloring where each series has a different color. Applying a DataPointStyle causes them all to turn orange.
Until someone suggests a better method, I've manually set the colors. I guess I won't be using the automatic palette for now.
<Style
x:Key="SimpleDataPointStyle"
BasedOn="{StaticResource {x:Type charting:LineDataPoint}}"
TargetType="{x:Type charting:LineDataPoint}">
<Setter Property="Width" Value="20" />
<Setter Property="Height" Value="20" />
</Style>
...
<chart:LineSeries ... >
<chart:DataPointSeries.DataPointStyle>
<Style
BasedOn="{StaticResource SimpleDataPointStyle}"
TargetType="{x:Type charting:LineDataPoint}">
<Setter Property="Background" Value="Green" />
</Style>
</chart:DataPointSeries.DataPointStyle>
</chart:LineSeries>
<chart:LineSeries ... >
<chart:DataPointSeries.DataPointStyle>
<Style
BasedOn="{StaticResource SimpleDataPointStyle}"
TargetType="{x:Type charting:LineDataPoint}">
<Setter Property="Background" Value="Red" />
</Style>
</chart:DataPointSeries.DataPointStyle>
</chart:LineSeries>
For those interested this can also be done in the code behind that adds a new LineSeries as follows:
ResourceDictionary rd = MyChart.Palette[MyChart.Series.Count % MyChart.Palette.Count];
Style style = new Style(typeof(LineDataPoint), rd["DataPointStyle"] as Style);
style.Setters.Add(new Setter(OpacityProperty, 0.0));
LineSeries ls = new LineSeries()
{
DataPointStyle = style
};
MyChart.Series.Add(ls);
instead of <Setter Property="Background" Value="Green" />
just bind the value to color as a property of the model. So <Setter Property="Background" Value="{Binding Path=Color}" />

Override style in style

Is it possible to override styles in other styles. My best description will be some non working code:
<Style x:Key="SpecialFont" TargetType="Label">
<Setter Property="Foreground" Value="Red" />
<Setter Property="FontSize" Value="28" />
</Style>
<Style TargetType="GroupBox">
<Setter Property="GroupBox.Resources">
<Setter.Value>
<Style x:Key="SpecialFont" TargetType="Label">
<Setter Property="FontSize" Value="74" />
</Style>
</Setter.Value>
</Setter>
</Style>
The idea is that I will define a style to my "special text", the font which by default is red and have a size at 28, but if the label is placed in a groupbox it should have the size at 74, but maintain the red color. How is this possible? I would prefer to have to same style-key in my xaml, and not create a style based on another, e.g. SpecialFontBig based on SpecialFont.
Edit:
Okay... Another explanation.
I want result like this:
<Style x:Key="BaseFont" TargetType="Label">
<Setter Property="Foreground" Value="White" />
</Style>
<Style x:Key="Font1" TargetType="Label" BasedOn="{StaticResource BaseFont}">
<Setter Property="FontSize" Value="10" />
</Style>
<Style x:Key="Font2" TargetType="Label" BasedOn="{StaticResource BaseFont}">
<Setter Property="FontSize" Value="20" />
</Style>
<Style x:Key="Font3" TargetType="Label" BasedOn="{StaticResource BaseFont}">
<Setter Property="FontSize" Value="30" />
</Style>
<Style x:Key="Font1Red" TargetType="Label" BasedOn="{StaticResource Font1}">
<Setter Property="Foreground" Value="Red" />
</Style>
<Style x:Key="Font2Red" TargetType="Label" BasedOn="{StaticResource Font2}">
<Setter Property="Foreground" Value="Red" />
</Style>
<Style x:Key="Font3Red" TargetType="Label" BasedOn="{StaticResource Font3}">
<Setter Property="Foreground" Value="Red" />
</Style>
Where FontX is used outside my groupboxes, and FontXRed is used inside them. Is it possible to overrule this foreground, without making a lot of FontXRed styles? For example something like:
<Style x:Key="BaseFont" TargetType="Label">
# IF INSIDE A GROUPBOX
<Setter Property="Foreground" Value="Red" />
# ELSE
<Setter Property="Foreground" Value="White" />
</Style>
Styles can be based on other styles -> http://msdn.microsoft.com/en-us/library/system.windows.style.basedon.aspx
<Style x:Key="Style1">
<Setter Property="Control.Background" Value="Yellow"/>
</Style>
<Style x:Key="Style2" BasedOn="{StaticResource Style1}">
<Setter Property="Control.Foreground" Value="Blue"/>
</Style>
I have created a new GroupBox, which solved my problem okay:
class MyGroupBox : GroupBox
{
public MyGroupBox()
{
var newForegroundSetter = new Setter(ForegroundProperty, Brushes.Black);
var stylesToUpdate = new List<string>
{
"TextBlockShared",
"SmallFontTextBlock",
"MediumFontTextBlock",
"LargeFontTextBlock",
"FontControlShared",
"SmallFontControl",
"SmallFontHeaderControl",
"MediumFontControl",
"MediumFontHeaderControl",
"LargeFontControl",
"LargeFontHeaderControl",
"SmallButton",
"MediumButton",
"LargeButton",
};
foreach (var styleKey in stylesToUpdate)
{
var existingStyle = FindResource(styleKey) as Style;
if (existingStyle == null) continue;
var newStyle = new Style(existingStyle.TargetType, existingStyle);
newStyle.Setters.Add(newForegroundSetter);
Resources.Add(styleKey, newStyle);
}
}
}
In case someone stumbles upon this, here is the trick that usually works for me.
Basically, I define a style inside another styles resources. I usually base the inner style on a key-referenced style to use it in other places, but you could place a plain simple style in there as well.
<Style x:Key="SpecialFont" TargetType="Label">
<Setter Property="Foreground" Value="Red" />
<Setter Property="FontSize" Value="28" />
</Style>
<Style TargetType="GroupBox">
<Style.Resources>
<Style TargetType="Label"
BasedOn="{StaticResource SpecialFont}" />
</Style.Resources>
</Style>

Default Image Style

It was my understanding that this:
<Style TargetType="{x:Type Image}">
<Setter Property="Margin" Value="0"/>
<Setter Property="Stretch" Value="None"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Cursor" Value="Help"/>
</Style>
would change the default image settings for each image rendered on the Window. Is this not the case? Is there an exception for images as it appears to work with other controls?
TIA
To set a style for a type you actually need to set the x:Key attribute like so:
<Style x:Key="{x:Type Image}" TargetType="{x:Type Image}">
<!-- setters here -->
</Style>

Resources