具有相同自定义属性的不同控件的通用样式

发布于 2024-12-03 02:33:15 字数 2455 浏览 1 评论 0原文

我有几个自定义控件,它们具有一些共同的自定义属性,并且我想应用使用这些属性触发的通用样式。我将这些通用属性放在一个接口(IMyControl)中,并让我的控件实现它,但似乎样式不能有一个作为 TargetType 的接口...

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

<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 和 Background 画笔,然后尝试使用:

<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 ,表示

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

指向该行的属性“System.Windows.Setter.Value”:

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

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

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

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

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>

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>

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

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

pointing at the line:
<Setter Property="local:IMyControl.Background" Value="{StaticResource FocusedBG}" />

It has an inner exception with the following message:

Value cannot be null. Parameter name: property'

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暮年慕年 2024-12-10 02:33:15

Setter 仅对 依赖属性 进行操作,在无法注册它们的界面中,而且它们只能注册一次,因此即使您在界面中为它们指定了某些字段,这也可能不是一个好主意。您可能需要考虑使用附加属性或使用基类其上的该属性(这可能不是一个选项)。

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

月棠 2024-12-10 02:33:15

如果您还在样式中设置“local:IMyControl.Background”的默认值,例如:

<Style x:Key="ControlRellenableEstilo">

    <Setter Property="local:IMyControl.Background" Value="Gray" /> 
    <!--or use TemplateBinding to Parent to get suitable color-->

    <Style.Triggers>

        ... skipped ...

    </Style.Triggers>
</Style>

另请检查 优先级列表

What about if you set a default value for 'local:IMyControl.Background' in the Style also, like:

<Style x:Key="ControlRellenableEstilo">

    <Setter Property="local:IMyControl.Background" Value="Gray" /> 
    <!--or use TemplateBinding to Parent to get suitable color-->

    <Style.Triggers>

        ... skipped ...

    </Style.Triggers>
</Style>

Also check a Precedence List on your CustomControl.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文