将 WPF 样式触发器绑定到自定义依赖项属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11758836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 04:48:23  来源:igfitidea点击:

Binding a WPF Style Trigger to a custom dependency property

wpftriggersstylesdependencies

提问by WPFNewbie

I have found numerous similar threads here, but none that seem to address my specific issue.

我在这里找到了许多类似的线程,但似乎没有一个能解决我的具体问题。

I need to highlight the background of a textbox under certain conditions. I have created a Highlight property and tried using a trigger in a style to set it but it doesn't actually ever highlight the text.

我需要在某些条件下突出显示文本框的背景。我创建了一个 Highlight 属性并尝试在样式中使用触发器来设置它,但它实际上并没有突出显示文本。

Here is my Style, simplified:

这是我的风格,简化:

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

Elements is defined as:

元素定义为:

xmlns:Elements="clr-namespace:MDTCommon.Controls.Forms.Elements">

Then I have the section where the style is applied:

然后我有应用样式的部分:

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

In the code behind of the DataElement class is the following:

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); }
}

A DataElement ultimately derived from UserControl and it contains a reference to TextBox object as well as othe objects.

DataElement 最终派生自 UserControl,它包含对 TextBox 对象以及其他对象的引用。

In the CustomForm class that houses all of the DataElement objects I have the following to set the color.

在包含所有 DataElement 对象的 CustomForm 类中,我有以下设置颜色。

Resources["EntryBoxHighlightBackground"] = Brushes.Yellow;

So, the first issue is that setting the Highlight property for the DataElement doesn't cause the textbox background to draw in yellow.

因此,第一个问题是为 DataElement 设置 Highlight 属性不会导致文本框背景绘制为黄色。

The other issue is that I realize that I am applying this style to all textboxes and I could have textboxes in other areas that are not actually contained within a DataElement, which may cause a binding issue.

另一个问题是,我意识到我正在将这种样式应用于所有文本框,并且我可能在其他区域中有文本框,而这些文本框实际上并未包含在 DataElement 中,这可能会导致绑定问题。

采纳答案by WPFNewbie Wannabe

Try converting your trigger to a DataTrigger, and add a binding that will look directly at the DataElement control, like so:

尝试将您的触发器转换为 DataTrigger,并添加将直接查看 DataElement 控件的绑定,如下所示:

<DataTrigger Binding="{Binding Path=Highlight, RelativeSource={RelativeSource AncestorType={x:Type Elements:DataElement}}}" Value="True">
    <Setter Property="Control.Background" Value="{DynamicResource EntryBoxHighlightBackground}"/>
</DataTrigger>