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>
Related
Here is my Application ResourceDictionary
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:CABI_PO_Manager.Themes">
<Style TargetType="TextBlock">
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="FontFamily" Value="Comic Sans MS"/>
<Setter Property="FontSize" Value="14"/>
</Style>
<Style BasedOn="{StaticResource {x:Type TextBlock}}"
TargetType="TextBlock"
x:Key="YellowTextBlock">
<Setter Property="FontSize" Value="16"/>
<Setter Property="Foreground" Value="#d8b243"/>
</Style>
<Style BasedOn="{StaticResource {x:Type TextBlock}}"
TargetType="TextBlock"
x:Key="GreenTextBlock">
<Setter Property="FontSize" Value="18"/>
<Setter Property="Foreground" Value="Green"/>
</Style>
<Style BasedOn="{StaticResource {x:Type TextBlock}}"
TargetType="TextBlock"
x:Key="RedTextBlock">
<Setter Property="FontSize" Value="14"/>
<Setter Property="Foreground" Value="#a01e21"/>
</Style>
</ResourceDictionary>
I want to have some default styles for a TextBlock which works
<Style TargetType="TextBlock">
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="FontFamily" Value="Comic Sans MS"/>
<Setter Property="FontSize" Value="14"/>
</Style>
I found somewhere to use x:Key but I can't get it to work.
I will have several TextBlock's, How do I identify a TextBlock in the UI XAML as Red, Yellow or Green TextBlock and apply that style to them? This isn't recognized x:Key="GreenTextBlock"
<TextBlock x:Key="GreenTextBlock" Grid.Column="1" Margin="0,10,0,0" TextWrapping="Wrap" Text="PO Manager" VerticalAlignment="Top" TextAlignment="Center" FontWeight="ExtraBold"/>
If u want apply style on for example all TextBlocks in application, just use style without x:key defined e.g
<Style TargetType="{x:Type TextBlock}">
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="FontFamily" Value="Comic Sans MS"/>
<Setter Property="FontSize" Value="14"/>
</Style>
When you are applying a style, use TargetType="{x:Type TextBlock}" instead of TargetType="TextBlock"
When do you use want to base on other style use
BasedOn="{StaticResource StyleUWantToBaseOn}"
where StyleUWantToBaseOn is style with x:Key property
And when you want to apply a specific style on lets say textblock you want to use Style property e.g:
<TextBlock Style="{StaticResource GreenTextBlock}" Grid.Column="1" />
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>
I have a <ResourceDictionary> containing this:
<Style TargetType="TextBlock">
<Setter Property="FontFamily" Value="..\..\Fonts\#Roboto"/>
<Setter Property="FontSize" Value="14"/>
<Setter Property="TextWrapping" Value="Wrap"/>
</Style>
This works fine.
Now I've added another style:
<Style x:Key="MyText" TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="Red"/>
<Setter Property="FontWeight" Value="SemiBold"/>
</Style>
However, after changing my <TextBlock> to <TextBlock Style="{StaticResource MyText}"/> - only the styles within the 2nd style block are picked up. E.g. the FontFamily, FontSize and TextWrapping are now ignored.
How can I have a default for a TextBlock, and then add to it? I don't want to add an x:Key to the 'default' style as this is in use throughout the system already.
I think you just need to base your keyed style on the type. See example below.
Note the BasedOn="{StaticResource {x:Type TextBlock}}"
<Window.Resources>
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="14"/>
<Setter Property="Foreground" Value="Green" />
<Setter Property="TextWrapping" Value="Wrap"/>
</Style>
<Style x:Key="MyText" TargetType="TextBlock">
<Setter Property="Foreground" Value="Red"/>
<Setter Property="FontWeight" Value="SemiBold"/>
</Style>
<Style x:Key="MyAppendedStyles" TargetType="TextBlock" BasedOn="{StaticResource {x:Type TextBlock}}">
<Setter Property="Foreground" Value="Red"/>
<Setter Property="FontWeight" Value="SemiBold"/>
</Style>
</Window.Resources>
<StackPanel>
<TextBlock Text="Hello" />
<TextBlock Style="{StaticResource MyText}" Text="Style Key" />
<TextBlock Style="{StaticResource MyAppendedStyles}" Text="Style Key" />
</StackPanel>
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}" />
In Silverlight 4 it's possible to use implicit styling - and that is amazing! But what if I want to apply a style to all of my Buttons, CheckBoxes and RadioButtons (all inheriting from ButtonBase)? I can't set TargetType on the Style to ButtonBase - that doesn't work. Do I need to create a style to each of the 3 control types?
http://www.silverlightshow.net/items/Implicit-Styles-in-Silverlight-4.aspx
Try this
xamlgeek,
The following implicit styles work well for me. I first create some name/keyed styles, using common BasedOn styles whereever possible. Then I simply create the implicit styles BasedOn those named/keyed styles...
<Style x:Key="BaseStyle" TargetType="Control">
<Setter Property="FontFamily" Value="{StaticResource FontFamily}" />
<Setter Property="FontSize" Value="{StaticResource FontSize}" />
<Setter Property="Foreground" Value="{StaticResource FontBrush}" />
</Style>
<Style x:Key="BaseStyleCentered" TargetType="Control" BasedOn="{StaticResource BaseStyle}">
<Setter Property="VerticalAlignment" Value="Center" />
</Style>
<Style x:Key="CommonCheckBox" TargetType="CheckBox" BasedOn="{StaticResource BaseStyleCentered}">
<Setter Property="Cursor" Value="Hand" />
</Style>
<Style x:Key="CommonRadioButton" TargetType="RadioButton" BasedOn="{StaticResource BaseStyleCentered}">
<Setter Property="Cursor" Value="Hand" />
</Style>
<Style x:Key="CommonButton" TargetType="Button" BasedOn="{StaticResource BaseStyleCentered}">
<Setter Property="Cursor" Value="Hand" />
<Setter Property="Padding" Value="10,0,10,0" />
<Setter Property="MinWidth" Value="{StaticResource ButtonWidth}" />
<Setter Property="MinHeight" Value="{StaticResource ButtonHeight}" />
</Style>
<Style TargetType="CheckBox" BasedOn="{StaticResource CommonCheckBox}">
</Style>
<Style TargetType="RadioButton" BasedOn="{StaticResource CommonRadioButton}">
</Style>
<Style TargetType="Button" BasedOn="{StaticResource CommonButton}">
</Style>
Good luck,
Jim
YinYangMe, YinYangMoney and FaceToFaceSoftware