gpt4 book ai didi

wpf - 样式触发器以应用另一种样式

转载 作者:行者123 更新时间:2023-12-01 23:34:36 25 4
gpt4 key购买 nike

我确信之前有人问过这个问题,但我还没有轻松地弄清楚如何用短语表达查询。

我有这个风格;

<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}" 添加到控件标记即可。但是,如果我想将这种样式与触发器一起应用,以便控件仅在特定条件下使用它(比如绑定(bind)为真)怎么办?像这样的东西:

<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 to affect the Style property of the object that it applies."

此代码抛出异常

这样的事能做吗?

编辑:如果这不能用样式触发器完成,因为它会覆盖自身,是否有另一种方法有条件地应用资源样式?

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

最佳答案

不能从 Style 中的 Setter 设置样式,因为那样的话基本上第一个 Style 根本不存在。

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

关于wpf - 样式触发器以应用另一种样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7519792/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com