c# - 使用 DataTrigger 以样式绑定(bind)到 Self

标签 c# wpf data-binding datatrigger

我有一个按钮样式。根据按钮是否启用,我想更改背景。这是它的样子:

<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 一起使用。我看到的只是一个灰色按钮。

这是跟踪:

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)



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

最佳答案

你为什么不把它改成触发器?

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

或者如果你想使用它,你不需要找到祖先,因为你当前在按钮上:
  <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 模型的代码。

关于c# - 使用 DataTrigger 以样式绑定(bind)到 Self,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39508943/

相关文章:

c# - WPF从按钮传递按钮内容?

c# - 如何创建wix卸载文件?

c# - 为什么我的数据绑定(bind)看到的是真实值而不是强制值?

WPF与窗口的RelativeSource绑定(bind)需要路径中的 "DataContext"?

javascript - Angular 数据绑定(bind)不适用于 async/await,但它可以用于 promises

c# - Unity如何让我的枪在玩家移动时停止射击?

c# - .NET CF 为组合框设置 'DroppedDown'

wpf - MVVM - 如何根据列表中的选定项目打开窗口?

c# - 如何隐藏数据表中的列

c# - Foreach 上 IEnumerable<> 与 if 条件