WPF :? 可重用的图像按钮模板?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18481787/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:31:24  来源:igfitidea点击:

WPF :?Re-usable template for image buttons?

c#wpfbutton

提问by Shugah

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?

我的WPF项目使用了很多图片按钮,但是由于我还没有找到合适的方法(我每次都要写相同的触发器和样式,唯一不同的是图片来源),我的资源字典变得很长不求回报。有没有更好的方法来做到这一点?

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>

Thank you :)

谢谢 :)

回答by Thomas Levesque

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

最简单的方法是创建一个带有Image属性的特定控件:

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

}

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

然后你只需在 generic.xaml 中为它创建一个样式,并绑定到Image属性而不是显式设置图像:

<Style x:Key="{x:Type my:ImageButton}" TargetType="{x:Type my:ImageButton}">
    <!-- Some setters -->
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type my:ImageButton}">
                <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 postfor details.

另一种可能的方法是使用我所谓的“参数化样式”,以避免创建特定控件;有关详细信息,请参阅此博客文章

回答by dave

You could try to create a custom control that inherits from Button, and use it as TargetType in your template. Then you can use TemplateBinding in the Image Source.

您可以尝试创建一个继承自 Button 的自定义控件,并将其用作模板中的 TargetType。然后你可以在 Image Source 中使用 TemplateBinding。

<Image Source="{TemplateBinding ImageSource}" Stretch="Fill"/>

You will need to create the ImageSource property in your custom control. That way you can set the source in xaml, but you need only one resource template.

您需要在自定义控件中创建 ImageSource 属性。这样您就可以在 xaml 中设置源,但您只需要一个资源模板。

回答by Florian Baierl

You can use the BasedOn Attribute like this:

您可以像这样使用 BasedOn 属性:

<Style x:Key="BlueHighlightedButtonStyle"  BasedOn="{StaticResource ButtonStyle}" TargetType="Button">
    <Setter Property="Background" Value="DeepSkyBlue"/>
</Style>

This will make a Button Style with the same attributes as your ButtonStyle, but with a DeepSkyBlue Background.

这将使 Button Style 具有与 ButtonStyle 相同的属性,但具有 DeepSkyBlue 背景。