四、WPF 切换按钮背景图样式写法
<!--1V按钮样式--> <Style TargetType="Button" x:Key="UButtonStyleOneVideo"> <Setter Property="Height" Value="20" /> <Setter Property="Width" Value="20" /> <Setter Property="BorderThickness" Value="0" /> <Setter Property="Cursor" Value="Hand"></Setter> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Button"> <Image Name="img" Source="/images/1nor.png" /> <ControlTemplate.Triggers> <Trigger Property="IsMouseOver" Value="True" > <Setter Property="Source" TargetName="img" Value="/images/1sel.png"></Setter> </Trigger> <Trigger Property="IsPressed" Value="True" > <Setter Property="Source" TargetName="img" Value="/images/1sel.png"></Setter> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style>
默认一张图片,通过触发器修改背景图
如果新增文件,记得在App.xaml导入样式
<Application x:Class="XX.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:XX.Zhifa" StartupUri="LoginWin.xaml"> <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="Style/UButton.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> </Application>
在窗口中引入样式
<Button Name="btnOneCamera" VerticalAlignment="Center" Content="" Style="{DynamicResource UButtonStyleOneVideo}"Margin="0 0 0 0" >