应用另一种样式的样式触发器

发布于 2024-12-06 12:52:15 字数 1369 浏览 1 评论 0原文

我确信以前已经有人问过这个问题,但我并没有很容易地弄清楚如何表达这个查询。

我有这个风格;

<SolidColorBrush x:Key="SemiTransparentRedBrushKey">#F0FF0000</SolidColorBrush>
<Style x:Key="TextBoxEmptyError" TargetType="{x:Type TextBox}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Text.Length}" Value="0">
            <Setter Property="BorderBrush" Value="{StaticResource ResourceKey=SemiTransparentRedBrushKey}"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

我可以将文本框应用为在文本框为空时具有红色边框。太棒了,我只需将 Style="{StaticResource TextBoxEmptyError}" 添加到控制标记即可。但是,如果我想通过触发器应用此样式,以便控件仅在某些条件下使用它(例如绑定为 true),该怎么办?像这样的东西:

<TextBox.Style>
<Style TargetType="TextBox">
    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=ApprovedRequired}" Value="True">
            <Setter Property="Style" Value="{StaticResource TextBoxEmptyError}"></Setter>
        </DataTrigger>
    </Style.Triggers>
</Style>

此代码抛出异常,尽管 {"Style object is not allowed tofluence the Style property of the object to which it apply."}

这样的事情可以完成吗?

编辑:如果样式触发器无法完成此操作,因为它会覆盖自身,是否有其他方法有条件地应用资源样式?

编辑:如果此操作有更合适的术语,我可以更改问题标题。

I am sure this has been asked before, but I haven't had an easy time figuring out how to phrase the query.

I have this style;

<SolidColorBrush x:Key="SemiTransparentRedBrushKey">#F0FF0000</SolidColorBrush>
<Style x:Key="TextBoxEmptyError" TargetType="{x:Type TextBox}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Text.Length}" Value="0">
            <Setter Property="BorderBrush" Value="{StaticResource ResourceKey=SemiTransparentRedBrushKey}"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

That I can apply to Textboxes to have a red border when they are empty. Its great, I can just add Style="{StaticResource TextBoxEmptyError}" to the Control Tag. But what if I want to apply this style with a trigger, so that the control only used it under certain conditions (like a binding being true)? Something like:

<TextBox.Style>
<Style TargetType="TextBox">
    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=ApprovedRequired}" Value="True">
            <Setter Property="Style" Value="{StaticResource TextBoxEmptyError}"></Setter>
        </DataTrigger>
    </Style.Triggers>
</Style>

This code throws an exception though {"Style object is not allowed to affect the Style property of the object to which it applies."}

Can something like this be done?

Edit: If this cannot be done with a Style trigger because it would overwrite itself, is there another way to Conditionally apply a resource style?

Edit: I can change the question title if there is a more proper term for this action.

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

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

发布评论

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

评论(2

心舞飞扬 2024-12-13 12:52:15

样式不能从样式内的 Setter 进行设置,因为这样第一个样式基本上将永远不会存在。

由于您正在寻找验证样式,我建议您查看 Validation.ErrorTemplate,尽管如果这不起作用,您可以更改触发器,以便它修改特定属性,例如 BorderBrush 而不是 Style财产

Styles cannot be set from a Setter within the Style, because then essentially the first Style would never exist at all.

Since you're looking for a Validation style, I would recommend looking into Validation.ErrorTemplate, although if that doesn't work you can change your trigger so it modifies specific properties such as BorderBrush instead of the Style property

向日葵 2024-12-13 12:52:15

我会考虑使用带有 TemplateTrigger 的模板,您可以根据任何条件将样式更改为您喜欢的样式

i would think of using a Template with a TemplateTrigger and there you can change the style to what ever you like based on what ever condition

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