Static Resources
Static reosurces stworzone w danym oknie będą dostępne tylko w nim.
// Definicja zmiennych wewnątrz obiektu Window w MainWindow.xaml
<Window.Resources>
<SolidColorBrush x:Key="btnBgColor" Color="DarkOliveGreen"></SolidColorBrush>
<SolidColorBrush x:Key="btnTextColor" Color="White"></SolidColorBrush>
</Window.Resources>
// Przypisanie zmiennej do właściwości kontrolki
<Grid>
<Button
x:Name="MyBtn_Copy"
Foreground="{StaticResource ResourceKey=btnTextColor}"
Background="{StaticResource ResourceKey=btnBgColor}"
Content="Button"
Height="60"
Width="160" />
</Grid>
Zasoby globalne (dla całej aplikacji) należy dodać w pliku App.xaml
// Deifnicja zasobów w obiekcie Application w pliku App.xaml
<Application.Resources>
<SolidColorBrush x:Key="btnBgColor" Color="DarkOliveGreen"></SolidColorBrush>
<SolidColorBrush x:Key="btnTextColor" Color="White"></SolidColorBrush>
</Application.Resources>
// Zasoby stworzone w App.xaml będą dostępne w całej aplikacji
<Button
x:Name="MyBtn_Copy"
Foreground="{StaticResource ResourceKey=btnTextColor}"
Background="{StaticResource ResourceKey=btnBgColor}"
Content="Button"
Height="60"
Width="160" />
Definiowanie styli – Implicit
// Definicja styli w App.xaml
<Style TargetType="Button">
<Setter Property="Foreground" Value="White"/>
<Setter Property="Background" Value="DarkOliveGreen"/>
<Setter Property="FontSize" Value="20"/>
</Style>
Definiowanie styli – Explicit
// Definicja styli z nazwą myStyle
<Application.Resources>
<SolidColorBrush x:Key="btnBgColor" Color="DarkOliveGreen"></SolidColorBrush>
<SolidColorBrush x:Key="btnTextColor" Color="White"></SolidColorBrush>
<Style TargetType="Button" x:Key="myStyle">
<Setter Property="Foreground" Value="White"/>
<Setter Property="Background" Value="{StaticResource btnBgColor}"/>
<Setter Property="FontSize" Value="20"/>
</Style>
</Application.Resources>
//Wykorzystanie styli w kontrolkach
<Button
x:Name="MyBtn_Copy"
Style="{StaticResource myStyle}"
Content="Button"
Height="60"
Width="160" />
Wiele styli bazujących na stylu głównym
// App.xaml
<Application.Resources>
<SolidColorBrush x:Key="btnBgColor" Color="DarkOliveGreen"></SolidColorBrush>
<SolidColorBrush x:Key="btnTextColor" Color="White"></SolidColorBrush>
<Style TargetType="Button" x:Key="myStyle">
<Setter Property="Foreground" Value="White"/>
<Setter Property="Background" Value="{StaticResource btnBgColor}"/>
<Setter Property="FontSize" Value="20"/>
</Style>
<Style TargetType="Button" x:Key="myStyle2" BasedOn="{StaticResource myStyle}">
<Setter Property="Foreground" Value="Yellow"/>
<Setter Property="Background" Value="{StaticResource btnBgColor}"/>
<Setter Property="FontSize" Value="20"/>
</Style>
</Application.Resources>
//Wykorzystanie w kontrolkach
<Button
x:Name="MyBtn_Copy"
Style="{StaticResource myStyle}"
Content="Button"
Height="60"
Width="160" />
<Button
x:Name="MyBtn_Copy1"
Style="{StaticResource ResourceKey=myStyle2}"
Content="Button"
Height="60"
Width="160" Margin="70,373,70,151" />