Style样式
- 1. 用法介绍
- 2. 样式多样性
- 3. 全局样式说明和资源字典的使用
1. 用法介绍
<Window x:Class="WPF_Study_Solution.window3"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WPF_Study_Solution"
mc:Ignorable="d"
Title="window3" Height="450" Width="800">
<Window.Resources>
<Style TargetType="Button">
<Setter Property="Background" Value="Green"/>
<Setter Property="FontSize" Value="20"/>
<Setter Property="Width" Value="200"/>
<Setter Property="Height" Value="30"/>
</Style>
</Window.Resources>
<StackPanel>
<Button Content="登录"/>
<Button Content="退出"/>
<Button Content="忘记密码"/>
</StackPanel>
</Window>
2. 样式多样性
- 写一个共有的样式
- 在基于这个样式写新的样式
- 类似于继承
<Window x:Class="WPF_Study_Solution.window3"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WPF_Study_Solution"
mc:Ignorable="d"
Title="window3" Height="450" Width="800">
<Window.Resources>
<Style TargetType="Button">
<Setter Property="Background" Value="WhiteSmoke"/>
<Setter Property="FontSize" Value="20"/>
<Setter Property="Margin" Value="20,10"/>
</Style>
<Style x:Key="LoginStyle" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="Background" Value="Green"/>
</Style>
<Style x:Key="QuitStyle" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="Background" Value="Red"/>
</Style>
</Window.Resources>
<StackPanel>
<Button Style="{StaticResource LoginStyle}" Content="登录"/>
<Button Style="{StaticResource QuitStyle}" Content="退出"/>
<Button Content="忘记密码"/>
</StackPanel>
</Window>
3. 全局样式说明和资源字典的使用
- 新建 资源字典 ,在资源字典中写全局Style样式 ,这样这个工程下的所有界面都可以使用这个 Style.
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPF_Study_Solution">
<Style TargetType="Button">
<Setter Property="Background" Value="WhiteSmoke"/>
<Setter Property="FontSize" Value="20"/>
<Setter Property="Margin" Value="20,10"/>
</Style>
<Style x:Key="LoginStyle" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="Background" Value="Green"/>
</Style>
<Style x:Key="QuitStyle" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="Background" Value="Red"/>
</Style>
</ResourceDictionary>
<Application x:Class="WPF_Study_Solution.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPF_Study_Solution"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/WPF_Study_Solution;component/BaseButtonStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
<Window x:Class="WPF_Study_Solution.window3"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WPF_Study_Solution"
mc:Ignorable="d"
Title="window3" Height="450" Width="800">
<StackPanel>
<Button Style="{StaticResource LoginStyle}" Content="登录"/>
<Button Style="{StaticResource QuitStyle}" Content="退出"/>
<Button Content="忘记密码"/>
</StackPanel>
</Window>