为window.resources样式创建自定义绑定属性 [英] Create custom binding property for window.resources style

查看:53
本文介绍了为window.resources样式创建自定义绑定属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前为自定义控件创建了几个自定义绑定,但是由于这种情况是针对window.resources的按钮样式(而不是控件模板),所以我不知道从哪里开始后面的代码.我将在哪里创建视图模型,它将继承或引用什么?

I've created a couple of custom bindings for a custom control before, but since this case is for a window.resources style for a button, (A control template rather), I don't know where to start for the code behind. Where would I create the viewmodel, and what would it inherit from or reference?

XAML:

<Style x:Key="UnifiedButtonStyle" TargetType="{x:Type Button}">
        <Setter Property="OverridesDefaultStyle" Value="True"/>
        <Setter Property="Margin" Value="{Binding Margin}"/>

        <Setter Property="Background" Value="#FFDDDDDD"/>
        <Setter Property="BorderBrush" Value="#FF707070"/>
        <Setter Property="Foreground" Value="#FF000000"/>

        <Setter Property="BorderThickness" Value="1"/>

        <Setter Property="Content" Value="Button"/>

        <Setter Property="Width" Value="75"/>
        <Setter Property="Height" Value="20"/>
        <Setter Property="Padding" Value="5"/>

        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>

        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid
                        x:Name="ButtonGrid"
                        Background="{TemplateBinding Background}"
                        OpacityMask="{TemplateBinding OpacityMask}">
                        <Border
                            x:Name="ButtonBorder"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            OpacityMask="{TemplateBinding OpacityMask}"

                            BorderThickness="{TemplateBinding BorderThickness}">

                            <Label
                                x:Name="ButtonLabel"
                                Foreground="{TemplateBinding Foreground}"

                                Padding="{TemplateBinding Padding}">
                                <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                                  VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                            </Label>
                        </Border>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter TargetName="ButtonGrid" Property="Background" Value="{Binding HoverColorBackground}"/>
                            <Setter TargetName="ButtonBorder" Property="BorderBrush" Value="{Binding HoverColorBorder}"/>
                            <Setter TargetName="ButtonLabel" Property="Foreground" Value="{Binding HoverColorForeground}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

悬空色彩设置器是这里的关键

Hovercolor setters are the key here

推荐答案

创建视图模型以自定义wpf按钮颜色是错误的方法.按钮配色方案严格属于视图.同样,许多按钮意味着许多视图模型实例,因为每个按钮可能都希望是唯一的-这样简单的设置需要太多代码.

Creating a viewmodel to customize wpf button colors is a wrong approach. Button color scheme is something which belongs strictly to a view. Also many buttons means many view model instances, since each button might want to be unique - too much code for such a simple setting.

按钮类没有足够的依赖项属性来设置表示HoverColorBackground/HoverColorBorder/HoverColorForeground的颜色.替代方法是创建派生的Button类(当DP为某种复杂类型和/或具有相关逻辑时要走的路)或使用附加的属性.我写了一个技巧,其中涵盖了第二种方法.

Button class doesn't have enough dependency properties to set color representing HoverColorBackground/HoverColorBorder/HoverColorForeground. Alternatives are to create a derived Button class (way to go when DP are of some complex type and/or have associated logic) or use attached properties. I have written a tip, which coveres the second approach.

短版

创建附加的DP

public static class Alt
{
    #region Background
    public static readonly DependencyProperty BackgroundProperty =
              DependencyProperty.RegisterAttached("Background", typeof(Brush),
              typeof(Alt), new PropertyMetadata(null));

    public static Brush GetBackground(DependencyObject obj)
    {
        return (Brush)obj.GetValue(Alt.BackgroundProperty);
    }

    public static void SetBackground(DependencyObject obj, Brush value)
    {
        obj.SetValue(Alt.BackgroundProperty, value);
    }
    #endregion
}

设置该属性的自定义值

<Button Content="Blue" Foreground="White" Margin="5"
        Background="Blue" ui:Alt.Background="DarkBlue"/>

确保模板知道如何使用该属性

make sure that template know how to use that property

<Trigger Property="IsMouseOver" Value="True">
    <Setter TargetName="ButtonGrid" 
            Property="Background" 
            Value="{Binding Path=(ui:Alt.Background),
                            RelativeSource={RelativeSource TemplatedParent}}"/>
</Trigger>

适用于任何控件.许多DP可以任意组合混合.

Works for any control. Many DP can be mixed in any combination.

这篇关于为window.resources样式创建自定义绑定属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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