WPF - 如何创建与模板图像按钮 [英] WPF - How to create image button with template
问题描述
我想创建一个按钮,其中有3张图片:一个正常的形象,一个pressed图像和残疾人的图像(我将使用这些创建向上/向下箭头按钮)
I am trying to create a button which has 3 images: a Normal image, a Pressed image and a Disabled image (I will use these for creating up/down arrow buttons).
我相信正确的做法是从按钮得出
,并使用模板
,并设置触发改变图片。我有3个依赖属性,每一个形象。
I believe the correct approach would be to derive from Button
and use a Template
and set triggers to change the image. I have 3 dependency properties, one for each image.
的图像将被的.png和具有透明背景(因为它们不是矩形)的
The images would be .png and have transparent backgrounds (as they are not rectangular).
我要寻找像的CBitmapButton
在MFC中。
I am looking for something like CBitmapButton
in MFC.
推荐答案
由于您从按钮继承你不需要依赖属性
。你已经有pssed 和 IsEnabled
属性是$ P $。事实上,这是所有你需要:
You won't need dependency properties because you are inheriting from Button
. You already have the IsPressed
and IsEnabled
properties. In fact, this is all you need:
<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>
通过您的用户控件code隐藏文件:
With your UserControl code-behind file:
public partial class ThreeStateImageButton : Button
{
public ThreeStateImageButton()
{
InitializeComponent();
}
}
这篇关于WPF - 如何创建与模板图像按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!