我正在尝试创建一个具有3个图像的按钮:正常图像,按下的图像和禁用的图像(我将使用它们来创建向上/向下箭头按钮).
我相信正确的方法是派生Button
并使用a Template
和set触发器来改变图像.我有3个依赖属性,每个图像一个.
图像为.png并具有透明背景(因为它们不是矩形).
我正在寻找类似于CBitmapButton
MFC的东西.
Cha*_*lie 71
您将不需要依赖项属性,因为您继承自Button
.你已经有了IsPressed
和IsEnabled
性能.事实上,这就是你所需要的:
<Button x:Class="TestWpfApplication.ThreeStateImageButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<Grid>
<Image Name="Normal" Source="Resources/Normal.png"/>
<Image Name="Pressed" Source="Resources/Pressed.png" Visibility="Hidden"/>
<Image Name="Disabled" Source="Resources/Disabled.png" Visibility="Hidden"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="Normal" Property="Visibility" Value="Hidden"/>
<Setter TargetName="Pressed" Property="Visibility" Value="Visible"/>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="Normal" Property="Visibility" Value="Hidden"/>
<Setter TargetName="Disabled" Property="Visibility" Value="Visible"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Button.Template>
</Button>
Run Code Online (Sandbox Code Playgroud)
使用UserControl代码隐藏文件:
public partial class ThreeStateImageButton : Button
{
public ThreeStateImageButton()
{
InitializeComponent();
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
77682 次 |
最近记录: |