具有相同自定义属性的不同控件的通用样式 [英] Common style for different controls with the same custom properties

查看:90
本文介绍了具有相同自定义属性的不同控件的通用样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个具有某些自定义属性的自定义控件,我想应用一种可触发这些属性的通用样式。我将这些通用属性放在接口(IMyControl)中,并让控件实现了它,但是看来样式不能像TargetType那样具有接口...

I have several custom controls that have in common some custom properties and I would like to apply a common style that triggers with those properties. I put those common properties in an interface (IMyControl) and made my controls implement it, but it seems that a style can't have an interface as TargetType...

我的自定义属性是两个布尔值:bMandatory和bIncomplete。常见样式部分是:

My custom properties are two booleans: bMandatory and bIncomplete. The common style part is:

<Style.Triggers>
    <Trigger Property="IsKeyboardFocusWithin" Value="True">
        <Setter Property="Background" Value="{StaticResource FocusedBG}" />
    </Trigger>
    <Trigger Property="bMandatory" Value="True">
        <Setter Property="Background" Value="{StaticResource MandatoryBG}" />
    </Trigger>
    <MultiTrigger>
        <MultiTrigger.Conditions>
            <Condition Property="bMandatory" Value="True" />
            <Condition Property="bIncomplete" Value="True" />
        </MultiTrigger.Conditions>
        <Setter Property="Background" Value="{StaticResource IncompleteBG}" />
    </MultiTrigger>
</Style.Triggers>

我还将接口 IsKeyboardFocusWithin bool和背景刷,然后尝试:

I also put in the interface the IsKeyboardFocusWithin bool and the Background brush and then tried with:

<Style x:Key="ControlRellenableEstilo">
    <Style.Triggers>
        <Trigger Property="local:IMyControl.IsKeyboardFocusWithin" Value="True">
            <Setter Property="local:IMyControl.Background" Value="{StaticResource FocusedBG}" />
        </Trigger>
        <Trigger Property="local:IMyControl.bMandatory" Value="True">
            <Setter Property="local:IMyControl.Background" Value="{StaticResource MandatoryBG}" />
        </Trigger>
        <MultiTrigger>
            <MultiTrigger.Conditions>
                <Condition Property="local:IMyControl.bMandatory" Value="True" />
                <Condition Property="local:IMyControl.bIncomplete" Value="True" />
            </MultiTrigger.Conditions>
            <Setter Property="Background" Value="{StaticResource IncompleteBG}" />
        </MultiTrigger>
    </Style.Triggers>
</Style>

在构建时不会抱怨,但是在执行应用程序时会抛出XamlParseException,该异常表示

It doesn't complain when building but, when the application is executed, it throws a XamlParseException that says


无法分配给属性'System.Windows.Setter.Value'

Failed to assign to property 'System.Windows.Setter.Value'

指向该行:
< Setter Property = local:IMyControl.Background Value = {StaticResource FocusedBG} />

它具有一个内部异常,并显示以下消息:

It has an inner exception with the following message:


值不能为空。参数名称:property'

Value cannot be null. Parameter name: property'

Value = {StaticResource FocusedBG} 部分在其余部分中起作用我的样式...
知道如何解决这个问题吗?

The Value="{StaticResource FocusedBG}" part is working in the rest of my styles... Any idea how could I solve this?

推荐答案

设置者可以在依赖性属性,在界面中您无法注册它们,并且它们只能注册一次,因此即使您在界面中为它们指定了一些字段,也可能不是一个好主意。您可能要考虑使用附加属性或将基类与该属性(可能不是一种选择)。

Setters operate on dependency properties only, in an interface you cannot register them, and they can only be registered once, so even if you specify some field for them in the interface that probably is far from a good idea. You might want to consider using attached properties or using a base-class with that property on it (which probably is not an option).

这篇关于具有相同自定义属性的不同控件的通用样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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