我有一个按钮的样式。根据按钮是否启用,我想改变背景。它是这个样子的:
<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 : 为绑定(hash=6303779)创建了BindingExpression (hash=31767240)
System.Windows.Data Warning: 58 : 路径: 'IsEnabled'
System.Windows.Data Warning: 60 : BindingExpression (hash=31767240): 默认模式已解析为OneWay
System.Windows.Data Warning: 61 : BindingExpression (hash=31767240): 默认更新触发器已解析为PropertyChanged
System.Windows.Data Warning: 62 : BindingExpression (hash=31767240): 已附加到System.Windows.Controls.Button.NoTarget (hash=24311680)
System.Windows.Data Warning: 66 : BindingExpression (hash=31767240): RelativeSource (FindAncestor) 需要树上下文
System.Windows.Data Warning: 65 : BindingExpression (hash=31767240): 解析源被延迟
System.Windows.Data Warning: 67 : BindingExpression (hash=31767240): 正在解析源
System.Windows.Data Warning: 70 : BindingExpression (hash=31767240): 找到数据上下文元素: (OK)
System.Windows.Data Warning: 73 : 查找类型为Button的祖先: 查询Grid (hash=35377238)
System.Windows.Data Warning: 73 : 查找类型为Button的祖先: 查询ContentPresenter (hash=51189900)
System.Windows.Data Warning: 73 : 查找类型为Button的祖先: 查询Border (hash=48541090)
System.Windows.Data Warning: 73 : 查找类型为Button的祖先: 查询StartStopControl (hash=22721178)
System.Windows.Data Warning: 73 : 查找类型为Button的祖先: 查询Grid (hash=32321338)
System.Windows.Data Warning: 73 : 查找类型为Button的祖先: 查询ContentPresenter (hash=31184590)
System.Windows.Data Warning: 73 : 查找类型为Button的祖先: 查询Border (hash=37117888)
System.Windows.Data Warning: 73 : 查找类型为Button的祖先: 查询MenuPanelControl (hash=873549)
System.Windows.Data Warning: 73 : 查找类型为Button的祖先: 查询Grid (hash=29953511)
System.Windows.Data Warning: 73 : 查找类型为Button的祖先: 查询ContentPresenter (hash=42576376)
System.Windows.Data Warning: 73 : 查找类型为Button的祖先: 查询AdornerDecorator (hash=66649760)
System.Windows.Data Warning: 73 : 查找类型为Button的祖先: 查询Border (hash=23566381)
System.Windows.Data Warning: 73 : 查找类型为Button的祖先: 查询MainWindow (hash=38392424)
看起来它遍历整个可视树,从包含按钮的网格开始。为什么不从按钮开始呢?