[Using .NET Framework 4.5.1]
I have the following resource set in one of my WPF Windows:
<Window.Resources>
<Style TargetType="{x:Type FrameworkElement}" x:Key="baseStyle">
<Setter Property="Margin" Value="5"/>
<Setter Property="VerticalAlignment" Value="Center"/>
</Style>
<Style TargetType="{x:Type FrameworkElement}" x:Key="basePlusEnabled" BasedOn="{StaticResource baseStyle}">
<Setter Property="IsEnabled" Value="{Binding TestIsRunning, Mode=OneWay}"/>
</Style>
<Style TargetType="RadioButton" BasedOn="{StaticResource basePlusEnabled}">
<Setter Property="Padding" Value="4,-5,0,0"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="Foreground" Value="{Binding TestIsRunning,
Converter={StaticResource testIsRunningForegroundConverter}, Mode=OneWay}"/>
</Style>
</Window.Resources>
However, the Padding style is not being applied to the RadioButtons in the window. (The other properties in the style are getting applied.) If I explicitly specify the Padding in each RadioButton, then it works. What am I missing here?
EDIT:
Sample RadioButton instance:
<RadioButton Grid.Column="4" Content="Iowa" GroupName="Facility"
IsChecked="{Binding IowaFacilityChecked1, UpdateSourceTrigger=PropertyChanged,
Mode=TwoWay}"/>
change TargetType="{x:Type FrameworkElement}" to TargetType="{x:Type RadioButton}"
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 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'm trying to factorize some styles with common properties.
So I'm using style inheritance but seems like it's not fully implemented.
Say I have these two styles:
<Style TargetType="ProgressBar">
<Setter Property="Height" Value="50"></Setter>
<Setter Property="Margin" Value="10"></Setter>
</Style>
<Style TargetType="Rectangle">
<Setter Property="Height" Value="50"></Setter>
<Setter Property="Margin" Value="10"></Setter>
</Style>
Here is my first try:
<Style x:Key="BaseStyle" TargetType="FrameworkElement">
<Setter Property="Height" Value="50"></Setter>
<Setter Property="Margin" Value="10"></Setter>
</Style>
<Style BasedOn="{StaticResource BaseStyle}" TargetType="ProgressBar"></Style>
<Style BasedOn="{StaticResource BaseStyle}" TargetType="Rectangle"></Style>
It's running but the Visual Studio designer is not happy and it displays this error:
Can only base on a Style with target type that is base type of this style's target type.
AFAIK if I trust MSDN both ProgressBar and Rectangle should inherit from FrameworkElement.
Here is the full code:
<StackPanel>
<StackPanel.Resources>
<!--<Style TargetType="ProgressBar">
<Setter Property="Height" Value="50"></Setter>
<Setter Property="Margin" Value="10"></Setter>
</Style>
<Style TargetType="Rectangle">
<Setter Property="Height" Value="50"></Setter>
<Setter Property="Margin" Value="10"></Setter>
</Style>-->
<Style x:Key="BaseStyle" TargetType="FrameworkElement">
<Setter Property="Height" Value="50"></Setter>
<Setter Property="Margin" Value="10"></Setter>
</Style>
<Style BasedOn="{StaticResource BaseStyle}" TargetType="ProgressBar"></Style>
<Style BasedOn="{StaticResource BaseStyle}" TargetType="Rectangle"></Style>
</StackPanel.Resources>
<ProgressBar Value="50"></ProgressBar>
<ProgressBar IsIndeterminate="True"></ProgressBar>
<Rectangle Width="100" Fill="BlueViolet"></Rectangle>
</StackPanel>
Note that the exact same code is working fine with WPF both in the designer and at runtime.
Is this a VS bug or a WinRT limitation?
Am I doing something wrong?
I have these styles defined:
<Style x:Key="ImgButton" TargetType="Button">
<Setter Property="Background" Value="Transparent"></Setter>
<Setter Property="Height" Value="30"></Setter>
</Style>
<Style x:Key="ImgButtonWithText" TargetType="{x:Type Button}" BasedOn="{StaticResource ImgButton}">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Name="ButtonImage" Stretch="Uniform" Height="20" Width="20" Source="">
</Image>
<TextBlock Name="ButtonText" Margin="4,0" VerticalAlignment="Center" Text=""></TextBlock>
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
I want to create another style, LoginButtonWithText (shown below), that is based on ImgButtonWithText, and which sets the value of the Source property of the ButtonImage control in the ImgButtonWithText style. I want to do the same with the Text property of the ButtonText textblock.
<Style x:Key="LoginButtonWithText" TargetType="{x:Type Button}" BasedOn="{StaticResource ImgButtonWithText}">
<Setter Property="ToolTip" Value="Login"></Setter>
<Setter Property="IsDefault" Value="True"></Setter>
//How do I set the Image Source value and TextBlock Text value here?
</Style>
I found this question, but it doesn't appear to be quite exactly what I'm looking for. How do I change the value of child controls from an inherited style, or am I doing it all wrong?
It seems that what I want to do is not exactly possible, but I've done the next best thing in defining an image button style to use in all styles that inherit from from ImgButtonWithText:
<Style x:Key="ImgButton" TargetType="Button">
<Setter Property="Background" Value="Transparent"></Setter>
<Setter Property="Height" Value="30"></Setter>
</Style>
<Style x:Key="ButtonImage" TargetType="Image">
<Setter Property="Height" Value="20"></Setter>
<Setter Property="Width" Value="20"></Setter>
<Setter Property="Stretch" Value="Uniform"></Setter>
</Style>
<Style x:Key="ButtonText" TargetType="TextBlock">
<Setter Property="Margin" Value="4,0"></Setter>
<Setter Property="VerticalAlignment" Value="Center"></Setter>
</Style>
<Style x:Key="LoginButtonWithText" TargetType="{x:Type Button}" BasedOn="{StaticResource ImgButton}">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Name="ButtonImage" Style="{StaticResource ButtonImage}" Source="../Images/login.png">
</Image>
<TextBlock Name="ButtonText" Style="{StaticResource ButtonText}" Text="Let's Go"></TextBlock>
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
When the user enters info I want all the controls to look and act the same way, as a result I have done the following.
The label and textbox controls are in a stackpanel as:
<StackPanel Style="{StaticResource ResourceKey=myInput}" HorizontalAlignment="Left">
<Label Content="Label" Name="label1" />
<TextBox Name="textBox1" ></TextBox>
</StackPanel>
the style "myInput" is:
<Style x:Key="myInput" TargetType="{x:Type StackPanel}">
<Style.Resources>
<Style TargetType="{x:Type Label}" BasedOn="{StaticResource {x:Type Label}}">
<Setter Property="FontSize" Value="12" />
</Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Margin" Value="5,-5,2,2"></Setter>
<Setter Property="Height" Value="23"/>
<Setter Property="VerticalAlignment" Value="Top"/>
<Style.Triggers>
<Trigger Property="IsFocused" Value="true">
<Setter Property="Background" Value="Blue" >
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</Style.Resources>
</Style>
Now every time that I apply that style to a stackpanel that has a label and a textbox the background of the textbox changes to blue when it receives focus.
How could I set the label's fontweight to bold also when that event fires? I will like to do this with xaml.
Use a DataTrigger on your Label which looks to see if the keyboard focus is inside of the parent StackPanel that contains both objects
<Style TargetType="{x:Type Label}" BasedOn="{StaticResource {x:Type Label}}">
<Setter Property="FontSize" Value="12" />
<Style.Triggers>
<DataTrigger Value="True" Binding="{Binding IsKeyboardFocusWithin,
RelativeSource={RelativeSource AncestorType={x:Type StackPanel}}}">
<Setter Property="FontWeight" Value="Bold" />
</Trigger>
</Style.Triggers>
</Style>