wpf的样式可以把属性一样的设置写在样式里面,这样可以减少代码量。
举个简单的例子:
<Window x:Class="WpfPictureClick.MainWindow"
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:WpfPictureClick"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<Style x:Key="BigFrontStyle">
<Setter Property="Control.FontFamily" Value="Time New Roman"></Setter>
<Setter Property="Control.FontSize" Value="18"></Setter>
</Style>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Button Grid.Row="0" Style="{StaticResource BigFrontStyle}" Width="80" Height="80" Content="3333"></Button>
<Button Grid.Row="1" Width="80" Height="80" Content="3333"></Button>
</Grid>
</Window>
样式需要在Window.Resources里面定义,然后在下面使用,直接上结果的图片
明显字体是不一样的,这个时候,样式的作用就体现出来了
*****************************除了设置属性之后,样式还能够设置事件*************
<Window x:Class="WpfPictureClick.MainWindow"
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:WpfPictureClick"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<Style x:Key="EventStyle">
<EventSetter Event="Button.MouseEnter" Handler="FrameworkElement_MouseEnter"></EventSetter>
<EventSetter Event="Button.MouseLeave" Handler="FrameworkElement_MouseLeave"></EventSetter>
</Style>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Button Grid.Row="0" Style="{StaticResource EventStyle}" Width="80" Height="80" Content="3333"></Button>
<Button Grid.Row="1" Width="80" Height="80" Content="3333"></Button>
</Grid>
</Window>
通过EventSetter来设置
处理函数如下:
public void FrameworkElement_MouseEnter(object sender, MouseEventArgs e)
{
((Button)sender).Background = new SolidColorBrush(Color.FromArgb(0xFF, 0x2c, 0x2c, 0x2c));
}
private void FrameworkElement_MouseLeave(object sender, MouseEventArgs e)
{
((Button)sender).Background = new SolidColorBrush(Color.FromArgb(0xFF, 0x12, 0x57, 0x90));
}
结果如下:
*****************************除了设置事件之后,样式有个王牌,叫触发器*************
<Window x:Class="WpfPictureClick.MainWindow"
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:WpfPictureClick"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<Style x:Key="EventStyle">
<EventSetter Event="Button.MouseEnter" Handler="FrameworkElement_MouseEnter"></EventSetter>
<EventSetter Event="Button.MouseLeave" Handler="FrameworkElement_MouseLeave"></EventSetter>
</Style>
<Style x:Key="TriggerStyle" >
<Style.Triggers>
<Trigger Property="Button.IsMouseOver" Value="True">
<Setter Property="Button.Background" Value="red"></Setter>
</Trigger>
<Trigger Property="Button.IsMouseOver" Value="false">
<Setter Property="Button.Background" Value="Black"></Setter>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Button Grid.Row="0" Focusable="False" Style="{StaticResource TriggerStyle}" Width="80" Height="80" Content="3333"></Button>
<Button Grid.Row="1" Width="80" Height="80" Content="3333"></Button>
</Grid>
</Window>
通过Style.Triggers来设置触发器,效果如下: