我有一个按钮样式。根据Button是否启用,我想更改背景。这就是它的样子:
<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警告:56:为绑定创建BindingExpression(hash = 31767240)(hash = 6303779)
System.Windows.Data警告:58:路径:'IsEnabled'
System.Windows.Data警告:60:BindingExpression(hash = 31767240):默认模式已解决为OneWay
System.Windows.Data警告:61:BindingExpression(hash = 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的祖先:查询网格(哈希= 35377238)
System.Windows.Data警告:73:查找类型Button的祖先:查询的ContentPresenter(哈希= 51189900)
System.Windows.Data警告:73:查找类型Button的祖先:查询边框(哈希= 48541090)
System.Windows.Data警告:73:查找类型Button的祖先:查询StartStopControl(hash = 22721178)
System.Windows.Data警告:73:查找类型Button的祖先:查询网格(哈希= 32321338)
System.Windows.Data警告:73:查找类型Button的祖先:查询的ContentPresenter(hash = 31184590)
System.Windows.Data警告:73:查找类型Button的祖先:查询边框(哈希= 37117888)
System.Windows.Data警告:73:查找类型Button的祖先:查询MenuPanelControl(hash = 873549)
System.Windows.Data警告:73:查找类型Button的祖先:查询网格(哈希= 29953511)
System.Windows.Data警告:73:查找类型Button的祖先:查询ContentPresenter(hash = 42576376)
System.Windows.Data警告:73:查找类型Button的祖先:查询AdornerDecorator(hash = 66649760)
System.Windows.Data警告:73:查找类型Button的祖先:查询边框(哈希= 23566381)
System.Windows.Data警告:73:查找类型Button的祖先:查询MainWindow(hash = 38392424)
看起来它贯穿整个可视树,从放置Button的Grid开始。为什么它不是以Button开头的?
答案 0 :(得分:8)
为什么不将其更改为“触发器”?
<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 Model代码。