在WPF中使用Button有时候需要让button响应鼠标的几种状态,虽然它本身也有响应的处理,但有时希望在它上面贴图片,在几种状态时可以随状态切换图片。此处用的是资源字典
类似:
1 <Style x:Key="ButtonStyle34" TargetType="{x:Type Button}">
2 <Setter Property="Template">
3 <Setter.Value>
4 <ControlTemplate TargetType="{x:Type Button}">
5 <StackPanel Orientation="Horizontal" Background="Transparent" Margin="0,0,0,0" >
6 <Image Name="ImgBtnBg3" Margin="0,0,0,2" Source="/Resources/Res/Delete_I.png" Stretch="Fill" Width="54"/>
7 </StackPanel>
8 <ControlTemplate.Triggers>
9 <Trigger Property="IsMouseOver" Value="True">
10 <Setter Property="Source" Value="/Resources/Res/Geng_II.png" TargetName="ImgBtnBg3"/>
11 </Trigger>
12 <Trigger Property="IsPressed" Value="True">
13 <Setter Property="Source" Value="/Resources/Res/Geng_III.png" TargetName="ImgBtnBg3"/>
14 </Trigger>
15 <Trigger Property="IsEnabled" Value="False">
16 <Setter Property="Source" Value="/Resources/Res/Delete_I.png" TargetName="ImgBtnBg3"/>
17 </Trigger>
18 </ControlTemplate.Triggers>
19 </ControlTemplate>
20 </Setter.Value>
21 </Setter>
22
23 </Style>
在页面:
<Button Style="{DynamicResource ButtonStyle34}" >
不光只有Button是这样其实很多控件都可以这样的比如 TabControl 里的 TabItem 也可以做成类似状态,基本上资源字典和button一样
<Style x:Key="TabItemStyle6" TargetType="{x:Type TabItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}">
<StackPanel Orientation="Horizontal" Margin="0,0,0,0" >
<Image Name="ImgBtnBg3" Source="/Resources/VLCIcon/RssIcon/LocalPlay_I.png" Stretch="Fill" Width="144"/>
</StackPanel>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Source" Value="/Resources/VLCIcon/RssIcon/LocalPlay_II.png" TargetName="ImgBtnBg3"/>
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Source" Value="/Resources/VLCIcon/RssIcon/LocalPlay_III.png" TargetName="ImgBtnBg3"/>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Source" Value="/Resources/VLCIcon/RssIcon/LocalPlay_III.png" TargetName="ImgBtnBg3"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
页面
<TabItem Header="Tab1" Style="{DynamicResourceTabItemStyle6}" >