利用 WPF 的 Trigger 系统,也可以很简单的只使用xmal实现三态按钮。在Window或UserControl的资源中声明按钮的style并加入触发功能。使用的时候直接在button里复写style就可以了,废话不多说,直接上代码:
class="brush: xml; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;"><UserControl.Resources> <Style x:Key="threeStateButton" TargetType="{x:Type Button}"> <Setter Property="SnapsToDevicePixels" Value="True"/> <Setter Property="OverridesDefaultStyle" Value="True"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Button}"> <StackPanel Orientation="Horizontal" > <Image Name="ImgBtnBg3" Source="Skins/Default/action_normal.png" /> </StackPanel> <ControlTemplate.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Source" Value="Skins/Default/action_selected.png" TargetName="ImgBtnBg3"/> </Trigger> <Trigger Property="IsPressed" Value="True"> <Setter Property="Source" Value="Skins/Default/action_active.png" TargetName="ImgBtnBg3"/> </Trigger> <Trigger Property="IsEnabled" Value="False"> <Setter Property="Source" Value="Skins/Default/action_normal.png" TargetName="ImgBtnBg3"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> </UserControl.Resources>
使用时在button中引用:
<Button x:Name="m_btn" Content="Button" HorizontalAlignment="Center" Width="60" Height="60"
VerticalAlignment="Center" Style="{StaticResource threeStateButton}"/>