我确信之前已经询问过这个问题,但我没有简单的时间来搞清楚如何对查询进行短语处理。样式触发器以应用另一种样式
我有这种风格;
<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}"
。但是,如果我想将这种风格应用于触发器,那么控件仅在特定条件下使用它(如绑定为真)呢?喜欢的东西:
<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 to which it applies."}
的异常可以像这样做呢?
编辑:如果不能使用伴奏触发器来完成,因为它会覆盖本身,有没有有条件应用资源风格的另一种方式?
编辑:如果有更适合此操作的术语,我可以更改问题标题。
你有没有考虑过使用'Validation.ErrorTemplate'?请参阅:http://wpftutorial.net/DataValidation.html – CodingGorilla
是的,但我确定对于我正在使用的一些表单来说这太过分了。这个问题的答案也将更适用于风格的使用,而不仅仅是我的红色错误边框。 – Tyrsius