将WPF样式触发器绑定到自定义依赖项属性 [英] Binding a WPF Style Trigger to a custom dependency property

查看:572
本文介绍了将WPF样式触发器绑定到自定义依赖项属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里找到了许多类似的线程,但是似乎都没有解决我的特定问题。

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

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

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.

这是我的样式,经过简化:

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>

元素定义为:

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>

DataElement类后面的代码如下:

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

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

最终从UserControl派生的DataElement,它包含对TextBox对象的引用,如下所示:以及其他对象。

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

在容纳所有DataElement对象的CustomForm类中,我可以使用以下命令设置颜色。

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

Resources["EntryBoxHighlightBackground"] = Brushes.Yellow;

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

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

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

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.

推荐答案

尝试将触发器转换为DataTrigger,并添加一个看起来很漂亮的绑定直接在DataElement控件上,如下所示:

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

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

这篇关于将WPF样式触发器绑定到自定义依赖项属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆