I use MetroDark theme.
I use this code in xaml :
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Theme/MetroDark/MetroDark.MSControls.Core.Implicit.xaml" />
<ResourceDictionary Source="Theme/MetroDark/MetroDark.MSControls.Toolkit.Implicit.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style TargetType="{x:Type Label}" >
<Setter Property="FontFamily" Value="B titr" />
<Setter Property="FontSize" Value="13" />
<Setter Property="FontWeight" Value="Bold" />
</Style>
<Style TargetType="{x:Type TextBox}" >
<Setter Property="FontFamily" Value="B Nazanin" />
<Setter Property="FontSize" Value="16" />
<Setter Property="FontWeight" Value="Bold" />
</Style>
</ResourceDictionary>
</Application.Resources>
font family was changed correctly but the background color was changed too.
I just want to change the font family and font size, and another property (such as: background, border etc) and get effect from default theme (MetroDark).
How can I do that?
set base style in BasedOn attribute like this:
<Style TargetType="{x:Type Label}" BasedOn="{StaticResource {x:Type Label}}">
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
this way custom style inherits all settings from base style and can override some of them
Related
I'm need to change the font size of all text across the application.
I have tried doing as follows, but that doesn't work:-
<Style x:Key="fontsize" TargetType="{x:Type FrameworkElement}">
<Setter Property="Control.FontSize" Value="20"/>
</Style>
<Style TargetType="{x:Type FrameworkElement}" BasedOn="{StaticResource fontsize}"/>
When I try setting as follows then that works fine but doesn't get applied to all elements & needs to apply that for all different types of elements aperately.
<Style TargetType="TextBlock" BasedOn="{StaticResource fontsize}"/>
<Style TargetType="TextBox" BasedOn="{StaticResource fontsize}"/>
<Style TargetType="DataGridCell" BasedOn="{StaticResource fontsize}"/>
<Style TargetType="MenuItem" BasedOn="{StaticResource fontsize}"/>
<Style TargetType="DatePicker" BasedOn="{StaticResource fontsize}"/>
Also I would like to ask that, is there a way that I can override the Global Style for a particular element, like Heading text should be of different size on a user control?
in App.xaml
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="20"/>
<Setter Property="FontWeight" Value="Bold"/>
</Style>
Create a global style for the window in App.xaml.
<Application.Resources>
<Style x:Key="WindowStyle" TargetType="{x:Type Window}">
<Setter Property="FontStyle" Value="Italic" />
<Setter Property="FontSize" Value="24" />
<Setter Property="Foreground" Value="Green"/>
</Style>
</Application.Resources>
and set that style for the required windows.
<Window x:Class="YourNamespace.MainWindow" Style="{StaticResource WindowStyle}".....>
for overriding the style for a usercontrol
<local:UserControl1>
<local:UserControl1.Style>
<Style TargetType="UserControl">
<Setter Property="FontSize" Value="10"/>
</Style>
</local:UserControl1.Style>
</local:UserControl1>
There are two controls this involves.
You're maybe thinking "hey what about this cell or that calendar".
Their templates show text in a textblock.
When you set Header on a menuitem or content on a label, you get a textblock generated.
You therefore "only" need to set style on both textblock and textbox:
<Application.Resources>
<ResourceDictionary>
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="20"/>
</Style>
<Style TargetType="TextBox">
<Setter Property="FontSize" Value="20"/>
</Style>
</ResourceDictionary>
</Application.Resources>
</Application>
Having said that.
As Clemens pointed out.
The Font size and styling dependency properties are marked as inherits, so if you just have mainwindow then you could just set on that.
It's not just "obvious" that a label ends up with a textblock in it when you set content though. Similarly a menuitem and header. Hence I thought it worth posting this answer.
... by the term "programming lines" I mean parts of code .
I am currently creating an UI using c# and XAML. But the XAML code is getting longer and longer, so I realised that if I could somehow set inside the code or store separately, repeatable parts of code and use them every time I needed, the whole XAML code would be shorter and clearer.
For example, let's say that I have a specific label which I want to repeat in several points of the code:
<Label Name="myLabel" Content="something">
</Label>
How could I possibly apply and repeat that label inside my XAML code?
There is a quick example of how the XAML code can be shared between different views/windows. Create a ResourceDictionary, define the shared properties/styles/control templates, like this
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="{x:Type Label}">
<Setter Property="Foreground" Value="Black" />
<Setter Property="FontFamily" Value="Segoe UI" />
</Style>
<Style TargetType="{x:Type Button}">
<Setter Property="Foreground" Value="Black" />
<Setter Property="FontFamily" Value="Segoe UI" />
</Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Foreground" Value="Black" />
<Setter Property="FontFamily" Value="Segoe UI" />
</Style>
<Style TargetType="{x:Type ScrollBar}">
<Setter Property="Foreground" Value="Black" />
<Setter Property="FontFamily" Value="Segoe UI" />
</Style>
<Style TargetType="Label" x:Key="TitleStyle" BasedOn="{StaticResource {x:Type Label}}">
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="HorizontalAlignment" Value="Stretch" />
<Setter Property="VerticalAlignment" Value="Stretch"/>
<Setter Property="FontSize" Value="16" />
</Style>
</ResourceDictionary>
Than you can add this dictionary to App/Window MergedDictionaries to use them, like
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Styles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
Please, note that this is just a quick example to briefly explain the idea. You can also have look at Style.TargetType docs to see explanation between TargetType and x:Key in styles
This question already has answers here:
WPF global font size
(11 answers)
Closed 4 years ago.
I need to set FontSize property for all controls in my wpf-layout at once.
I mean I don't want to set it for labels, then for chechboxes etc. I want to set it for all controls which support this property.
So, in "Settings" of my module I have values of font size for buttons and for the rest of controls. For buttons I set font size this way:
<Style TargetType="Button">
<Setter Property="FontSize" Value="{Binding Source={x:Static properties:Settings.Default}, Path=ButtonFontSize}" />
</Style>
Now I need to set FontSize for the rest of controls.
You can set it on the window i guess, as it should be inherited from the Parent Control.
<Style TargetType="{x:Type Window}">
<Setter Property="FontSize" Value="24" />
</Style>
Copied from this answer.
I'd do it this way:
<Window.Resources>
<Style TargetType="{x:Type Control}" x:Key="baseStyle">
<Setter Property="FontSize" Value="100" />
</Style>
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource baseStyle}"></Style>
<Style TargetType="{x:Type Label}" BasedOn="{StaticResource baseStyle}"></Style>
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource baseStyle}"></Style>
<Style TargetType="{x:Type ListView}" BasedOn="{StaticResource baseStyle}"></Style>
<!-- ComboBox, RadioButton, CheckBox, etc... -->
</Window.Resources>
If you want to set the fontsize for all controls of one specific tipe you could use this
<Window.Resources>
<Style TargetType="{x:Type TextBox}">
<Setter Property="FontSize" Value="24" />
</Style>
<Style TargetType="{x:Type Textblock}">
<Setter Property="FontSize" Value="20" />
</Style>
</Window.Resources>
If you have many styles and want to keep the variable editable it makes sense to define it above like so:
<Window.Resources>
<System:Double x:Key="stdFontSize">15</System:Double>
<Style TargetType="{x:Type TextBox}">
<Setter Property="FontSize" Value="stdFontSize" />
</Style>
<Style TargetType="{x:Type Textblock}">
<Setter Property="FontSize" Value="stdFontSize" />
</Style>
</Window.Resources>
I'm using Mahapps.Metro in my Application.
I created a Custom Accent Resource Dictionary [according to this tutorial] and every things work well but i can't change Border Color of elements globally in my application.
I added following custom resource (BorderBrush) to my custom accent resource dictionary but it can't change anythings:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
............... OTHER RESOURCES ..............
<Color x:Key="BorderColor">#666</Color>
<SolidColorBrush x:Key="BorderBrush" Color="{StaticResource BorderColor}"/>
</ResourceDictionary>
For example i want change Border Color of all Buttons...
How can i do this?
You should inherit from the base styles and make your changes.
Samles
<Color x:Key="CustomBorderColor">#666</Color>
<SolidColorBrush x:Key="CustomBorderBrush" Color="{StaticResource CustomBorderColor}"/>
<Style x:Key="CustomMetroCircleButtonStyle"
TargetType="{x:Type Button}"
BasedOn="{StaticResource MetroCircleButtonStyle}">
<Setter Property="BorderBrush" Value="{DynamicResource CustomBorderBrush}" />
</Style>
<!-- or -->
<Style x:Key="CustomMetroButton"
TargetType="{x:Type Button}"
BasedOn="{StaticResource MetroButton}">
<Setter Property="BorderBrush" Value="{DynamicResource CustomBorderBrush}" />
</Style>
<!-- or -->
<Style x:Key="CustomToggleButton"
TargetType="{x:Type Button}"
BasedOn="{StaticResource ToggleButton}">
<Setter Property="BorderBrush" Value="{DynamicResource CustomBorderBrush}" />
</Style>
<!-- usage -->
<Style TargetType="Button" BasedOn="{DynamicResource CustomMetroCircleButtonStyle}" />
<!-- or -->
<Style TargetType="Button" BasedOn="{DynamicResource CustomMetroButton}" />
<!-- or -->
<Style TargetType="Button" BasedOn="{DynamicResource CustomToggleButton}" />
etc...
Hope that helps!
I'm using Mahapps for a GUI, however I want to set some attributes different than visual ones such as margins and verticalAlignment, so I added this to the UserControl.resources section
<Style x:Key="{x:Type TextBox}" TargetType="TextBox" BasedOn="{StaticResource ResourceKey={x:Type TextBox}}">
<Setter Property="Margin" Value="2"/>
<Setter Property="VerticalAlignment" Value="Center"/>
</Style>
However it overrides all the visual styles attributes of the TextBoxes, how can I just add those attributes without overriding all the visual styles settings?
give the style a key
<Style x:Key="myCustomTextBoxStyle"
TargetType="TextBox"
BasedOn="{StaticResource ResourceKey={x:Type TextBox}}">
<Setter Property="Margin" Value="2"/>
<Setter Property="VerticalAlignment" Value="Center"/>
</Style>
and use it where you need it
<TextBox Style={StaticResource myCustomTextBoxStyle} />
EDIT
or put it to the main resource dictionary of user control or window resource without a key
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
</ResourceDictionary.MergedDictionaries>
<Style TargetType="TextBox"
BasedOn="{StaticResource ResourceKey={x:Type TextBox}}">
<Setter Property="Margin" Value="2"/>
<Setter Property="VerticalAlignment" Value="Center"/>
</Style>
</ResourceDictionary>
</Window.Resources>
hope that helps