wpf 使用 DataTrigger 以样式绑定到 Self

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39508943/
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 13:55:34  来源:igfitidea点击:

Binding to Self in Style with DataTrigger

c#wpfdata-bindingdatatrigger

提问by gartenriese

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>

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.

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

This is the trace:

这是跟踪:

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)

System.Windows.Data 警告:56:为绑定创建 BindingExpression(hash=31767240)(hash=6303779)
System.Windows.Data 警告:58:Path:'IsEnabled'
System.Windows.Data 警告:60:BindingExpression(hash= 31767240):默认模式解析为 OneWay
System.Windows.Data 警告:61:BindingExpression (hash=31767240):默认更新触发器解析为 PropertyChanged
System.Windows.Data 警告:62:BindingExpression (hash=31767240):附加到系统。 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):找到数据上下文元素:(OK)
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 : Button 类型的查找祖先: 查询 StartStopControl (hash=22721178)
System.Windows.Data 警告: 73 : Button 类型的查找祖先: 查询网格 (hash= 32321338)
System.Windows.Data 警告:73:Button 类型的查找祖先:查询 ContentPresenter (hash=31184590)
System.Windows.Data 警告:73:Button 类型的查找祖先:查询 Border (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 (hash=66649760)
System.Windows.Data 警告:73:按钮类型的查找祖先:查询的边框(哈希=23566381)
System.Windows.Data 警告:73:按钮类型的查找祖先:查询的 MainWindow(哈希 = 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?

看起来它遍历了整个可视化树,从放置 Button 的 Grid 开始。为什么不从 Button 开始?

回答by Mr.B

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>

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

如果它对您没有帮助,您应该提供更多详细信息:原始 XAML 代码以及您的视图模型代码。