我在这里发现了许多类似的线程,但没有一个似乎解决了我的具体问题.
我需要在某些条件下突出显示文本框的背景.我创建了一个Highlight属性,并尝试在样式中使用触发器来设置它,但它实际上并不突出显示文本.
这是我的风格,简化:
<Style x:Key="TextBoxStyle" BasedOn="{StaticResource CommonStyles}">
<Style.Triggers>
<Trigger Property="Elements:DataElement.Highlight" Value="True">
<Setter Property="Control.Background"
Value="{DynamicResource EntryBoxHighlightBackground}"/>
</Trigger>
</Style.Triggers>
</Style>
Run Code Online (Sandbox Code Playgroud)
元素定义为:
xmlns:Elements="clr-namespace:MDTCommon.Controls.Forms.Elements">
Run Code Online (Sandbox Code Playgroud)
然后我有应用样式的部分:
<!-- Applies above style to all TextBoxes -->
<Style TargetType="TextBox" BasedOn="{StaticResource TextBoxContentHolder}" >
<Setter Property="Validation.ErrorTemplate" Value="{x:Null}" />
<!-- Overrides the default Error Style -->
</Style>
Run Code Online (Sandbox Code Playgroud)
在DataElement类的后面代码如下:
public static readonly DependencyProperty HighlightProperty =
DependencyProperty.Register("Highlight", typeof(bool), typeof(DataElement));
public bool Highlight
{
get { return (bool)base.GetValue(HighlightProperty); }
set { base.SetValue(HighlightProperty, value); }
}
Run Code Online (Sandbox Code Playgroud)
DataElement最终派生自UserControl,它包含对TextBox对象以及其他对象的引用.
在容纳所有DataElement对象的CustomForm类中,我有以下内容来设置颜色.
Resources["EntryBoxHighlightBackground"] = Brushes.Yellow;
Run Code Online (Sandbox Code Playgroud)
因此,第一个问题是为DataElement设置Highlight属性不会导致文本框背景以黄色绘制.
另一个问题是我意识到我正在将这种样式应用于所有文本框,并且我可以在其他区域中具有实际上不包含在DataElement中的文本框,这可能会导致绑定问题.