我正在尝试创建一个具有3个图像的按钮:正常图像,按下的图像和禁用的图像(我将使用它们来创建向上/向下箭头按钮).
我相信正确的方法是派生Button并使用a Template和set触发器来改变图像.我有3个依赖属性,每个图像一个.
图像为.png并具有透明背景(因为它们不是矩形).
我正在寻找类似于CBitmapButtonMFC的东西.
我实现了一个带有图像的简单按钮:
<Button Command="{Binding ButtonCommand, ElementName=ImageButtonControl}">
<StackPanel Orientation="Horizontal">
<Image Source="{Binding ButtonImage, ElementName=ImageButtonControl}"/>
<TextBlock Text="{Binding ButtonText, ElementName=ImageButtonControl}" Margin="2,0,0,0"/>
</StackPanel>
</Button>
Run Code Online (Sandbox Code Playgroud)
如您所见,我公开了一个ButtonCommand属性,以便能够将ICommand附加到此UserControl:
public partial class ImageButton : UserControl
{
/// <summary>
/// The dependency property that gets or sets the source of the image to render.
/// </summary>
public static DependencyProperty ImageSourceProperty =
DependencyProperty.Register("ButtonImage", typeof(ImageSource), typeof(ImageButton));
public static DependencyProperty TextProperty =
DependencyProperty.Register("ButtonText", typeof(string), typeof(ImageButton));
public static DependencyProperty ButtonCommandProperty =
DependencyProperty.Register("ButtonCommand", typeof(ICommand), typeof(ImageButton));
public ImageButton()
{
this.DataContext = this;
InitializeComponent();
}
/// <summary>
/// …Run Code Online (Sandbox Code Playgroud)