Reputation: 757
Is there some way to set global styles in resource dictionary in xaml without having to specify any special settings in affected elements?
For example, let's say i have resdictionary.xaml file with
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
</ResourceDictionary>
And then I have normal window.xaml file:
<Window x:Class="App.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="500" Width="800" MinWidth="500" MinHeight="350">
<Window.Resources>
<ResourceDictionary Source="resdictionary.xaml" />
</Window.Resources>
<Button Content="Button 1"/>
<Button Content="Button 2"/>
<Button Content="Button 3"/>
</Window>
And I want all buttons to have black-white background gradient and Calibri font. Is there a way to specify this in the resdictionary.xaml without having to change the window.xaml file?
Upvotes: 1
Views: 3626
Reputation: 4040
You can do it the way Dream Coder has shown. But that way you have to set the style
for every button. If you do not set the style
to a button, that button
will use the default windows style. To do so, you have to remove the x:Key
from the style
.
<Style TargetType="{x:Type Button}">
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush EndPoint="0.5,1"
StartPoint="0.5,0">
<GradientStop Color="#000000" />
<GradientStop Color="#FFFFFF"
Offset="1" />
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Setter Property="FontFamily"
Value="Calibri" />
</Style>
Upvotes: 2
Reputation: 550
you can create a styles.xaml page and add all your styles there for example
<Style x:Key="SubmitButtonStyle" TargetType="Button">
<Setter Property="Height">
<Setter.Value>28px</Setter.Value>
</Setter>
<Setter Property="Width">
<Setter.Value>90px</Setter.Value>
</Setter>
<Setter Property="BorderThickness">
<Setter.Value>1</Setter.Value>
</Setter>
<Setter Property="BorderBrush">
<Setter.Value>#000000</Setter.Value>
</Setter>
<Setter Property="Background">
<Setter.Value>#0073c6</Setter.Value>
</Setter>
<Setter Property="FontFamily" >
<Setter.Value>Segoe UI</Setter.Value>
</Setter>
<Setter Property="FontSize">
<Setter.Value>12px</Setter.Value>
</Setter>
</Style>
you can refer the styles.xaml in your windows using the following code.
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="../Resources/Styles.xaml"></ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
you can use the style in following way in your page
<Button Style="{StaticResource TelerikRadSubmitButtonStyle}"/>
hope this helps.
Upvotes: 5