Application.Resources应用程序资源,资源:【样式】,图片,字体文件,音频,视频等
在此处定义的资源,是全局性的,整个应用程序都可以共享此资源
此处定义的样式,称为全局样式,整个应用程序都可用。 优先级最低。
局部样式:某个窗体内可用。优先级次之。
行内样式:某个控件可用,某个控件(标签),开始标签中的属性都是行内样式。 优先级最高。
<Application.Resources>
<ResourceDictionary>
<Style x:Key="buttonDefaultStyle" TargetType="Button">
<Setter Property="Background" Value="Blue" />
<Setter Property="Foreground" Value="White" />
</Style>
<Style x:Key="buttonWarnStyle" TargetType="Button">
<Setter Property="Background" Value="Red" />
<Setter Property="Foreground" Value="White" />
</Style>
<!-- 合并资源字典文件(导入样式文件) -->
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/WpfApp1;component/Resources/Dictionary1.xaml" />
<ResourceDictionary Source="pack://application:,,,/WpfApp1;component/Resources/Dictionary2.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
WpfApp1;component/Resources/Dictionary1.xaml 路径来源
Dictionary1.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="LabelStyle" TargetType="Label">
<Setter Property="Margin" Value="0,10" />
<Setter Property="Grid.Column" Value="0" />
<Setter Property="Grid.Row" Value="0" />
<Setter Property="Margin" Value="0,10" />
<Setter Property="Content" Value="_Account:" />
<Setter Property="Target" Value="{Binding ElementName=txtAccount}" />
</Style>
<Style
x:Key="LabelStyle2"
BasedOn="{StaticResource LabelStyle}"
TargetType="Label">
<Setter Property="Grid.Row" Value="1" />
<Setter Property="Content" Value="_Password:" />
<Setter Property="Target" Value="{Binding ElementName=txtPassword}" />
</Style>
</ResourceDictionary>
Dictionary2.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="ButtonBaseStyle" TargetType="Button">
<Setter Property="Width" Value="100" />
<Setter Property="Height" Value="30" />
</Style>
<Style
x:Key="ButtonStyle"
BasedOn="{StaticResource ButtonBaseStyle}"
TargetType="Button">
<Setter Property="Grid.Row" Value="2" />
<Setter Property="Grid.Column" Value="0" />
<Setter Property="Grid.ColumnSpan" Value="2" />
<Setter Property="Margin" Value="0,5" />
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="Background" Value="#399ae7" />
<Setter Property="BorderThickness" Value="0" />
<Setter Property="Content" Value="登录" />
<Setter Property="Foreground" Value="#ffffff" />
</Style>
<Style x:Key="BaseStyle" TargetType="Control">
<Setter Property="Width" Value="120" />
<Setter Property="Height" Value="30" />
<Setter Property="Padding" Value="5,0" />
</Style>
<Style
x:Key="TextBoxStyle"
BasedOn="{StaticResource BaseStyle}"
TargetType="TextBox">
<Setter Property="Grid.Row" Value="0" />
<Setter Property="Grid.Column" Value="1" />
<Setter Property="Width" Value="120" />
<Setter Property="Height" Value="30" />
<Setter Property="Padding" Value="5,0" />
<Setter Property="VerticalContentAlignment" Value="Center" />
</Style>
<Style
x:Key="PasswordBoxStyle"
BasedOn="{StaticResource BaseStyle}"
TargetType="PasswordBox">
<Setter Property="Grid.Row" Value="1" />
<Setter Property="Grid.Column" Value="1" />
<Setter Property="Width" Value="120" />
<Setter Property="Height" Value="30" />
<Setter Property="Padding" Value="5,0" />
<Setter Property="VerticalContentAlignment" Value="Center" />
</Style>
</ResourceDictionary>