wpf MultiTrigger:绑定到 DependencyProperty 的条件不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12551627/
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
MultiTrigger: Condition Binding to DependencyProperty doesn't work
提问by TorbenJ
I have a custom button MainMenuButtonof type UserControland atm I'm styling it.
Now I wanted to implement a MultiTriggerto only change the appearance of the button if two conditions are met.
我有一个自定义按钮MainMenuButton类型UserControl和 atm 我正在设计它。现在我想实现 aMultiTrigger仅在满足两个条件时更改按钮的外观。
The first condition is if IsMouseOver == true.
I simply put the following Condition:
第一个条件是 if IsMouseOver == true。我简单地提出以下几点Condition:
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsMouseOver" Value="True"/>
</MultiTrigger.Conditions>
<MultiTrigger.EnterActions>
<Setter TargetName="LayoutRoot" Property="Background" Value="Red">
</MultiTrigger.EnterActions>
<MultiTrigger.ExitActions>
<Setter TargetName="LayoutRoot" Property="Background" Value="Black">
</MultiTrigger.ExitActions>
</MultiTrigger>
The second condition is related to a DependencyProperty:
第二个条件与 a 相关DependencyProperty:
public static readonly DependencyProperty IsCheckedProperty = DependencyProperty.Register("IsChecked", typeof(bool), typeof(MainMenuButton), new PropertyMetadata(false));
In another SO posta user said that I can use DataTriggerto react to IsCheckedProperty.
So I tried the code from the other post but it didn't work:
在另一篇SO 帖子中,一位用户说我可以DataTrigger用来对IsCheckedProperty. 所以我尝试了另一篇文章中的代码,但没有奏效:
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsMouseOver" Value="True"/>
<Condition Binding="{Binding RelativeSource={RelativeSource Mode=Self}, Path=IsChecked}" Value="False"/>
</MultiTrigger.Conditions>
<MultiTrigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource MouseEnter}"/>
</MultiTrigger.EnterActions>
<MultiTrigger.ExitActions>
<BeginStoryboard Storyboard="{StaticResource MouseLeave}"/>
</MultiTrigger.ExitActions>
</MultiTrigger>
How can this be solved? Thanks for any answers! :)
如何解决这个问题?感谢您提供任何答案!:)
回答by TorbenJ
Got it to work in the mean time. I stumbled upon this blog article which contains a working solution: http://anders.janmyr.com/search?q=multidatatrigger
同时让它工作。我偶然发现了这篇包含有效解决方案的博客文章:http: //anders.janmyr.com/search?q=multidatatrigger
Changed my code to the following:
将我的代码更改为以下内容:
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsMouseOver}" Value="True"/>
<Condition Binding="{Binding RelativeSource={RelativeSource Mode=Self}, Path=IsChecked}" Value="True"/>
</MultiDataTrigger.Conditions>
<Setter TargetName="LayoutRoot" Property="Background" Value="Red"/>
</MultiDataTrigger>
Now it works. Anyway thanks to all answerers for their effort!
现在它起作用了。无论如何,感谢所有回答者的努力!
回答by KaluSinghRao
<Window x:Class="DataBinding.MultiTrigger"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MultiTrigger" Height="300" Width="300">
<Window.Resources>
<Style TargetType="Button">
<Style.Setters>
<Setter Property="Background" Value="BlueViolet"></Setter>
<Setter Property="Foreground" Value="Red"></Setter>
</Style.Setters>
<Style.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<!--Use only one conditions for Implementations-->
<Condition Property="Control.IsFocused" Value="True"></Condition>
<Condition Property="Control.IsMouseOver" Value="True"></Condition>
</MultiTrigger.Conditions>
<MultiTrigger.Setters>
<Setter Property="Background" Value="Green"></Setter>
<Setter Property="Foreground" Value="White"></Setter>
</MultiTrigger.Setters>
</MultiTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Button Height="50">The World Game</Button>
回答by Rohit Vats
I guess in your style you haven't specified the correct TargetType. This should work -
我猜在你的风格中你没有指定正确的TargetType. 这应该有效 -
<Style TargetType="{x:Type local:MainMenuButton}">
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsMouseOver" Value="True"/>
<Condition Binding="IsChecked" Value="False"/>
</MultiTrigger.Conditions>
// Your setter goes here
</MultiTrigger>
</Style>
Namespace localshould be added in xaml which corresponds to the namespace where your MainMenuButton class resides.
命名空间local应该添加到 xaml 中,它对应于 MainMenuButton 类所在的命名空间。
回答by Sebastian Dymel
I think you dont need to bind to your DP, try using just Property="IsChecked" as for the IsMouseOver.
我认为您不需要绑定到您的 DP,尝试仅使用 Property="IsChecked" 作为 IsMouseOver。

