使用DataTrigger绑定到样式自 [英] Binding to Self in Style with DataTrigger

查看:126
本文介绍了使用DataTrigger绑定到样式自的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个按钮样式。根据是否启用按钮,我想更改背景。看起来是这样:

I have a Style for a Button. Depending on if the Button is enabled or not, I want to change the Background. This is what it looks like:

<Style x:Key="MyButtonStyle" TargetType="Button">
    <Style.Triggers>
        <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Button}, Path=IsEnabled, PresentationTraceSources.TraceLevel=High}" Value="False">
            <Setter Property="Background" Value="Purple"/>
        </DataTrigger>
        <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Button}, Path=IsEnabled, PresentationTraceSources.TraceLevel=High}" Value="True">
            <Setter Property="Background" Value="Yellow"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

这只是一个基本示例。实际上,我需要一个MultiDataTrigger,但它甚至不能与常规DataTrigger一起使用。我所看到的只是一个灰色按钮。

This is just a basic example. Actually I need a MultiDataTrigger, but it's not even working with a regular DataTrigger. All I see is a gray button.

这是迹线:


System.Windows.Data警告:56:为绑定(hash = 6303779)创建了BindingExpression(哈希= 31767240)

System.Windows.Data警告:58:路径:'IsEnabled'

System.Windows.Data警告:60:BindingExpression(哈希= 31767240):默认模式已解析为OneWay

System.Windows.Data警告:61:BindingExpression(哈希= 31767240):默认更新触发器已解析为PropertyChanged

System.Windows.Data警告:62:BindingExpression(hash = 31767240):附加到System.Windows.Controls.Button.NoTarget(hash = 24311680)

System.Windows .Data警告:66:BindingExpression(hash = 31767240):RelativeSource(FindAncestor)需要树上下文

System.Windows.Data警告:65:BindingExpression(hash = 31767240):解析源延迟

System.Windows.Data警告:67:BindingExpression(hash = 31767240):解决了源

System.Windows.Data警告:70:BindingExpression(hash = 31767240):找到数据上下文元素:(确定)

System.Windows.Data警告:73:类型为Button的查找祖先:查询网格( hash = 35377238)

System.Windows.Data警告:73:类型为Button的查找祖先:查询的ContentPresenter(hash = 51189900)

System.Windows.Data警告:73:查找类型的Button的祖先:查询边框(hash = 48541090)
System.Windows.Data警告:73:类型的祖先的查询:查询StartStopControl(hash = 22721178)

系统。 Windows.Data警告:73:按钮类型的查找祖先:查询的网格(hash = 32321338)

System.Windows.Data警告:73:按钮类型的查找祖先:查询的ContentPresenter(hash = 31184590)< br>
System.Windows.Data警告:73:类型的查找祖先Button:查询边框(hash = 37117888)

System.Windows.Data警告:73:类型的查找祖先但是吨:查询的MenuPanelControl(哈希= 873549)

System.Windows.Data警告:73:按钮类型的查找祖先:查询的网格(哈希= 29953511)

System.Windows.Data警告:73:按钮类型的查找祖先:查询的ContentPresenter(hash = 42576376)

System.Windows.Data警告:73:按钮类型的查找祖先:查询的AdornerDecorator(哈希= 66649760)

System.Windows.Data警告:73:按钮类型的查找祖先:查询边框(hash = 23566381)

System.Windows.Data警告:73:按钮类型的查找祖先:查询的MainWindow( hash = 38392424)

System.Windows.Data Warning: 56 : Created BindingExpression (hash=31767240) for Binding (hash=6303779)
System.Windows.Data Warning: 58 : Path: 'IsEnabled'
System.Windows.Data Warning: 60 : BindingExpression (hash=31767240): Default mode resolved to OneWay
System.Windows.Data Warning: 61 : BindingExpression (hash=31767240): Default update trigger resolved to PropertyChanged
System.Windows.Data Warning: 62 : BindingExpression (hash=31767240): Attach to System.Windows.Controls.Button.NoTarget (hash=24311680)
System.Windows.Data Warning: 66 : BindingExpression (hash=31767240): RelativeSource (FindAncestor) requires tree context
System.Windows.Data Warning: 65 : BindingExpression (hash=31767240): Resolve source deferred
System.Windows.Data Warning: 67 : BindingExpression (hash=31767240): Resolving source
System.Windows.Data Warning: 70 : BindingExpression (hash=31767240): Found data context element: (OK)
System.Windows.Data Warning: 73 : Lookup ancestor of type Button: queried Grid (hash=35377238)
System.Windows.Data Warning: 73 : Lookup ancestor of type Button: queried ContentPresenter (hash=51189900)
System.Windows.Data Warning: 73 : Lookup ancestor of type Button: queried Border (hash=48541090)
System.Windows.Data Warning: 73 : Lookup ancestor of type Button: queried StartStopControl (hash=22721178)
System.Windows.Data Warning: 73 : Lookup ancestor of type Button: queried Grid (hash=32321338)
System.Windows.Data Warning: 73 : Lookup ancestor of type Button: queried ContentPresenter (hash=31184590)
System.Windows.Data Warning: 73 : Lookup ancestor of type Button: queried Border (hash=37117888)
System.Windows.Data Warning: 73 : Lookup ancestor of type Button: queried MenuPanelControl (hash=873549)
System.Windows.Data Warning: 73 : Lookup ancestor of type Button: queried Grid (hash=29953511)
System.Windows.Data Warning: 73 : Lookup ancestor of type Button: queried ContentPresenter (hash=42576376)
System.Windows.Data Warning: 73 : Lookup ancestor of type Button: queried AdornerDecorator (hash=66649760)
System.Windows.Data Warning: 73 : Lookup ancestor of type Button: queried Border (hash=23566381)
System.Windows.Data Warning: 73 : Lookup ancestor of type Button: queried MainWindow (hash=38392424)

它看起来像遍历整个可视树,从按钮所在的网格开始。

It looks like it goes through the whole visual tree, starting with the Grid where the Button is placed in. Why does it not start with the Button?

推荐答案

为什么不将其更改为Trigger?

Why don't you change it to Trigger?

  <Style x:Key="MyButtonStyle"
           TargetType="Button">
        <Style.Triggers>
            <Trigger Property="IsEnabled"
                     Value="False">
                <Setter Property="Background"
                        Value="Purple" />
            </Trigger>
            <Trigger Property="IsEnabled"
                     Value="True">
                <Setter Property="Background"
                        Value="Yellow" />
            </Trigger>
        </Style.Triggers>
    </Style>

或者如果您仍要使用它,则无需寻找祖先,因为您当前在按钮上:

Or if you want to use it anyway, you don't need to find an ancestor, because you're currently on the button:

  <Style x:Key="MyButtonStyle"
           TargetType="Button">
        <Style.Triggers>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsEnabled, PresentationTraceSources.TraceLevel=High}"
                         Value="False">
                <Setter Property="Background"
                        Value="Purple" />
            </DataTrigger>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsEnabled, PresentationTraceSources.TraceLevel=High}"
                         Value="True">
                <Setter Property="Background"
                        Value="Yellow" />
            </DataTrigger>
        </Style.Triggers>
    </Style>

如果没有帮助,您应该提供更多详细信息:原始XAML代码,也许您的View模型的代码也是如此。

If it doesn't help you, you should give more details: Original XAML code and maybe your View Model's code too.

这篇关于使用DataTrigger绑定到样式自的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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