相关疑难解决方法(0)

WPF - 如何使用模板创建图像按钮

我正在尝试创建一个具有3个图像的按钮:正常图像,按下的图像和禁用的图像(我将使用它们来创建向上/向下箭头按钮).

我相信正确的方法是派生Button并使用a Template和set触发器来改变图像.我有3个依赖属性,每个图像一个.

图像为.png并具有透明背景(因为它们不是矩形).

我正在寻找类似于CBitmapButtonMFC的东西.

wpf templates image button

30
推荐指数
1
解决办法
8万
查看次数

在WPF UserControl中附加ICommand

我实现了一个带有图像的简单按钮:

    <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)

c# wpf user-controls mvvm

7
推荐指数
1
解决办法
2万
查看次数

标签 统计

wpf ×2

button ×1

c# ×1

image ×1

mvvm ×1

templates ×1

user-controls ×1