WPF:对图像按钮可重复使用的模板? [英] WPF : Re-usable template for image buttons?

查看:124
本文介绍了WPF:对图像按钮可重复使用的模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的WPF项目使用了大量的图像按钮的,但因为我还没有找到一种方法来正确地做到这一点(我每次写相同的触发器和风格,唯一不同的是图像源),我的资源字典成为没有什么很长。是否有这样做的更好的办法?

My WPF project uses a lot of image buttons, but since I haven't found a way to do it properly (I have to write the same triggers and style each time, only difference is the image source), my resource dictionary became very long for nothing. Is there a better way of doing this?

下面是我用我的按钮样式的示例:

Here's a sample of the style I'm using for my buttons :

<Style x:Key="ButtonStyle" TargetType="{x:Type Button}">
    <!-- Some setters -->
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Grid>
                    <Image Source="Images.png" Stretch="Fill"/>
                </Grid>
                <ControlTemplate.Triggers>
                    <!-- Some triggers ( IsFocused, IsMouseOver, etc.) -->
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

感谢您:)

推荐答案

最简单的方法是创建与图像特定的控制属性:

The easiest way is to create a specific control with an Image property:

public class ImageButton : Button
{
    static ImageButton()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof (ImageButton),
            new FrameworkPropertyMetadata(typeof (ImageButton)));
    }


    public ImageSource Image
    {
        get { return (ImageSource)GetValue(ImageProperty); }
        set { SetValue(ImageProperty, value); }
    }

    public static readonly DependencyProperty ImageProperty =
        DependencyProperty.Register("Image", typeof(ImageSource), typeof(ImageButton), new PropertyMetadata(default(ImageSource)));

}

然后你只需创建一个样式它在generic.xaml,并绑定到图片属性,而不是明确设置图像的:

Then you just create a style for it in generic.xaml, and bind to the Image property instead of setting the image explicitly:

<Style x:Key="{x:Type my:ImageButton}" TargetType="{x:Type my:ImageButton}">
    <!-- Some setters -->
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Grid>
                    <Image Source="{TemplateBinding Image}" Stretch="Fill"/>
                </Grid>
                <ControlTemplate.Triggers>
                    <!-- Some triggers ( IsFocused, IsMouseOver, etc.) -->
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

然后,你可以使用它是这样的:

Then you can just use it like this:

<my:ImageButton Image="Image.png" />

如果您需要为按钮的不同状态更多的图像,你可以将控件添加更多的依赖属性。

If you need more images for different states of the button, you can add more dependency properties to the control.

另一种可能的方法是使用我所说的参数化的风格,以避免产生特定的控制;看到这个博客帖子了解详情。

Another possible approach is to use what I call a "parameterized style", to avoid creating a specific control; see this blog post for details.

这篇关于WPF:对图像按钮可重复使用的模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆