Automatic Style Inheritance - wpf

I have a question about automatic style inheritance.
Basically, I have a superclass for which I would like to define the default style for textboxes and textblocks for classes that derive from it. These styles are based on my default styles as defined in Styles.xaml.
<controls:BaseUserControl.Resources>
<Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource DisplayLabel}">
<Setter Property="Foreground" Value="Black"/>
<Setter Property="Margin" Value="10,0,10,0"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="VerticalAlignment" Value="Center"/>
</Style>
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource InputField}">
<Setter Property="Height" Value="30"/>
<Setter Property="Margin" Value="10,0,0,0"></Setter>
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"></Setter>
</Style>
</controls:BaseUserControl.Resources>
This class is called DataEntryView.
Now when I derive from this class, the textblock and textboxes just get their default Windows look, not what I defined here.
<views:DataEntryView.Resources>
<!-- Do I need to add anything here? -->
</views:DataEntryView.Resources>
What is it that I'm forgetting?
Basically, I don't want to explicitly set the Style of every textblock and textbox to some key.

You need to place the styles in a RessourceDictionary and include this into the super class and the derived classes using the RessourceDictionary.MergedDictionaries property.
Styles file (Styles.xaml)
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" ....>
<Style ...> <!--Your styles goes here-->
</ResourceDictionary>
Controls (Parent and children):
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Styles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>

Related

Wpf - Create a Style in a resource dictionary that sets properties of children of a grid [duplicate]

Is it possible to define a ResourceDictionary in a Style?
For example, suppose I wanted to have two different Styles for StackPanels and in one I want all the buttons to be blue and the other I want them to be red. Is this possible?
Something like
<Style x:Key="RedButtonsPanel" TargetType="{x:Type StackPanel}">
<Setter Property="Orientation" Value="Horizontal" />
<Setter Property="StackPanel.Resources">
<Setter.Value>
<ResourceDictionary>
<Style TargetType="{x:Type Button}">
<Setter Property="Background" Value="Red" />
</Style>
</ResourceDictionary>
</Setter.Value>
</Setter>
</Style>
The above code fails with an error about the Property value of a Setter cannot be null (even though it's obviously not null).
I can do something like
<ResourceDictionary x:Key="RedButtons">
<Style TargetType="{x:Type Button}">
<Setter Property="Width" Value="100" />
<Setter Property="Background" Value="Red" />
</Style>
</ResourceDictionary>
<StackPanel Resources={StaticResource RedButtons} />
However I was wondering if there was a way to merge the ResourceDictionary into the style.
Try adding the Style(s) for each TargetType to the DockPanel Style.Resources.
I did something similar with a DockPanel Style. Wanted all Buttons or Separators added to the DockPanel to get styled in a consistent manner.
Here's a sample:
<Style x:Key="DockPanelToolBarStyle" TargetType="{x:Type DockPanel}">
<Style.Resources>
<Style TargetType="Button" BasedOn="{StaticResource ButtonToolBarStyle}" />
<Style TargetType="Separator" BasedOn="{StaticResource SeparatorToolBarStyle}" />
</Style.Resources>
<Setter Property="Height" Value="45"/>
<Setter Property="Background" Value="{StaticResource ToolBarBrush}"/>
</Style>
StackPanel.Resources is not a DependencyProperty and therefore I don't believe you will be able to set that property within the style.

Telerik Custom Style not reflecting when applied to RadButton

we are using telerik control in our WPF application. I am applying custom style for telerik control but it is not working.
<Style x:Key="RadButtonStyle" TargetType="telerik:RadButton">
<Setter Property="Cursor" Value="Hand"/>
<Setter Property="Width" Value="60"/>
<Setter Property="Height" Value="22"/>
<Setter Property="Margin" Value="3,3,5,3"/>
</Style>
<telerik:RadButton Style="{StaticResource RadButtonStyle}"
Content="Login"
Margin="5"
cmd:Click.Command="
{Binding LoginCommand}" />
but style is not getting applied. it is actually hiding the button...
What I did: I have downloaded NugetPackage for Telerik Theme and added reference
<ResourceDictionary Source="/Telerik.Windows.Themes.Office_Black;component/Themes/System.Windows.xaml"/>
<ResourceDictionary Source="/Telerik.Windows.Themes.Office_Black;component/Themes/Telerik.Windows.Controls.xaml"/>
<ResourceDictionary Source="/Telerik.Windows.Themes.Office_Black;component/Themes/Telerik.Windows.Controls.Input.xaml"/>
<ResourceDictionary Source="/Telerik.Windows.Themes.Office_Black;component/Themes/Telerik.Windows.Controls.GridView.xaml"/>
Try to base your Style on the implicit one using the BasedOn property:
<Style x:Key="RadButtonStyle" TargetType="telerik:RadButton"
BasedOn="{StaticResource {x:Type telerik:RadButton}}">
<Setter Property="Cursor" Value="Hand"/>
<Setter Property="Width" Value="60"/>
<Setter Property="Height" Value="22"/>
<Setter Property="Margin" Value="3,3,5,3"/>
</Style>

WPF: Moving panel resources to dictionary file

One of my Grid currently starts with the following code:
<Grid x:Name="Top_GRID" Margin="4.953,10" Width="817.28">
<Grid.Resources>
<Style TargetType="TextBlock">
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Margin" Value="3"/>
<Setter Property="Background" Value="Red" />
</Style>
<Style TargetType="TextBox">
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Margin" Value="3"/>
</Style>
<Style TargetType="Button">
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Margin" Value="3"/>
</Style>
</Grid.Resources>
Just to clarify - I want to declare a Grid in which all TextBlocks are having the Background property set to "Red". All Button margins are set to "3" and so on.
I'd like to move the resource definition to a dictionary file.
Should I somehow wrap it as a Style?
If so, I'm going to have a recursive style declaration which (I think is illegal).
Sounds simple but I can't find the way to do it.
Try this
<Style x:Key="Grid_ControlStyle" TargetType="Grid">
<Style.Resources>
<Style TargetType="TextBlock">
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Margin" Value="3"/>
<Setter Property="Background" Value="Red" />
</Style>
<Style TargetType="TextBox">
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Margin" Value="3"/>
</Style>
<Style TargetType="Button">
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Margin" Value="3"/>
</Style>
</Style.Resources>
</Style>
You Need to put all styles for button, TextBox etc. in resourceDictionary file. Then add this file in application resources:
<Application x:Class="Aplication.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources\YourResource.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Then in xaml you will be able to use it like this
<TextBlock Grid.Column="1" Grid.Row="1" Text="bla bla" Style="{DynamicResource YourStyle}"/>
Finally your style should look like
<Style x:Key="StyleName" TargetType="{x:Type TextBlock}">
<Setter Property="Margin" Value="3,3,3,3"/>
<Setter Property="FontFamily" Value="Arial"/>
<Setter Property="FontSize" Value="12pt"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Center"/>
</Style>
Hope thats what you was looking for.
Please go through the concept of resource dictionary in WPF. Any style, colors, fonts etc whatever is related to look of the application which you wish to be repeated in more than one screen of your application shpuld be placed in the resource dictionary.
x:Key is the property which can be used to access the style anywhere across the application.
For your resource dictionary to be accessible througout the application, place in it app.xaml

How to share wpf style setter

I have the following ResourceDictionary:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="StyleComboBox" TargetType="{x:Type ComboBox}">
<Setter Property="BorderBrush" Value="DarkGray" />
<Setter Property="BorderThickness" Value="1" />
<!-- Styles for ComboBox -->
</Style>
<Style x:Key="StyleTextBox" TargetType="{x:Type TextBox}">
<Setter Property="BorderBrush" Value="DarkGray" />
<Setter Property="BorderThickness" Value="1" />
<!-- Styles for Textbox -->
</Style>
</ResourceDictionary>
How is it possible to use only at one position the setter?
Styles in wpf can be inherited from another style.
<Style x:Key="baseStyle" TargetType="TextBlock">
<Setter Property="FontSize" Value="12" />
<Setter Property="Background" Value="Orange" />
</Style>
<Style x:Key="boldStyle" BasedOn="{StaticResource baseStyle}" TargetType="TextBlock">
<Setter Property="FontWeight" Value="Bold" />
</Style>
source
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="baseStyle" TargetType="Control">
<Setter Property="BorderBrush" Value="DarkGray" />
<Setter Property="BorderThickness" Value="1" />
</Style>
<Style x:Key="StyleComboBox" BasedOn="{StaticResource baseStyle}" TargetType="{x:Type ComboBox}">
<!-- Styles for ComboBox -->
</Style>
<Style x:Key="StyleTextBox" BasedOn="{StaticResource baseStyle}" TargetType="{x:Type TextBox}">
<!-- Styles for Textbox -->
</Style>
</ResourceDictionary>
<Style TargetType="Control" x:Key="Controlbase">
<Setter Property="Control.BorderThickness" Value="10"/>
</Style>
<Style x:Key="StyleComboBox" TargetType="{x:Type ComboBox}" BasedOn="{StaticResource Controlbase}">
<Setter Property="BorderBrush" Value="DarkGray" />
<!-- Styles for ComboBox -->
</Style>
<Style x:Key="StyleTextBox" TargetType="{x:Type TextBox}" BasedOn="{StaticResource Controlbase}">
<Setter Property="BorderBrush" Value="DarkGray" />
<!-- Styles for Textbox -->
</Style>
I hope this will help.
Curious if this worked for you. Need to be careful in that you're redefining the style of say a ComboBox as based on your Control base style. Presumably the control template is not affected by this as that would be bad. IOW A ComboBox is much more than just a simple control and needs to inherit the styles and preserve the control template of all that it means to be a ComboBox. IE Its a SelectorControl that inherits from an ItemsControl etc.
I wonder if rebasing its style would also cause it to prefer/use the default control template of a Control rather than retaining its "identity" as say a ComboBox.

WPF Change fontSize of button with style fails?

Well i have my file Styles.xaml thats merged in the Application.xaml so it applies to every thing..
here are my styles
<Style TargetType="{x:Type Control}" x:Key="baseStyle">
<Setter Property="FontFamily" Value="Verdana"/>
<Setter Property="FontSize" Value="12"/>
</Style>
<Style TargetType="Button" BasedOn="{StaticResource baseStyle}">
<Setter Property="Margin" Value="2,0,2,0"/>
<Setter Property="Padding" Value="2"/>
<Setter Property="FontSize" Value="50"/>
</Style>
<Style TargetType="TextBlock">
<Setter Property="FontFamily" Value="Verdana"/>
<Setter Property="FontSize" Value="12"/>
</Style>
When im in the editor this seems to work but when i run the application the font-size of the buttons are shrinked to their normal sizes..
My guess is that the buttons create a TextBlock when their content is set to a string and then use the textblock style.. but how can i override this?
You're right about
My guess is that the buttons create a
TextBlock when their content is set to
a string and then use the textblock
style
. See this post.
A workaround is to define a
DataTemplate for System.String, where
we can explicitly use a default
TextBlock to display the content. You
can place that DataTemplate in the
same dictionary you define the
TextBlock style so that this
DataTemplate will be applied to
whatever ContentPresenter effected by
your style.
So adding the DataTemplate at the end to Styles.xaml will fix the problem
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib">
<Style TargetType="{x:Type Control}" x:Key="baseStyle">
<Setter Property="FontFamily" Value="Verdana"/>
<Setter Property="FontSize" Value="12"/>
</Style>
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource baseStyle}">
<Setter Property="Margin" Value="2,0,2,0"/>
<Setter Property="Padding" Value="2"/>
<Setter Property="Foreground" Value="Red" />
<Setter Property="FontSize" Value="50"/>
</Style>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="FontFamily" Value="Verdana"/>
<Setter Property="Foreground" Value="Green" />
<Setter Property="FontSize" Value="24"/>
</Style>
<DataTemplate DataType="{x:Type sys:String}">
<TextBlock Text="{Binding}">
<TextBlock.Resources>
<Style TargetType="{x:Type TextBlock}"/>
</TextBlock.Resources>
</TextBlock>
</DataTemplate>
</ResourceDictionary>
This will keep your Style for a TextBlock but the TextBlock created in a Button for example won't be effected by it
I tried your styles, and it works well. So your styles are not the problem. I think it's the place you merged the style as you wrote. You'd better put your ResourceDictionary Styles.xaml in your MainWindow file instead of your Application.xaml.
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Styles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<StackPanel>
<TextBlock>TTT</TextBlock>
<Button>BBB</Button>
</StackPanel>
</Window>
But your problem remains unclear, if it's not the solution could you clarify a bit more the way you use your styles by posting this part of your code?

Resources