简体   繁体   English

MultiTrigger:条件绑定到 DependencyProperty 不起作用

[英]MultiTrigger: Condition Binding to DependencyProperty doesn't work

I have a custom button MainMenuButton of type UserControl and atm I'm styling it.我有一个UserControl类型的自定义按钮MainMenuButton和 atm 我正在设计它。 Now I wanted to implement a MultiTrigger to only change the appearance of the button if two conditions are met.现在我想实现一个MultiTrigger仅在满足两个条件时才更改按钮的外观。

The first condition is if IsMouseOver == true .第一个条件是IsMouseOver == true I simply put the following Condition :我简单地提出以下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 :第二个条件与DependencyProperty

public static readonly DependencyProperty IsCheckedProperty = DependencyProperty.Register("IsChecked", typeof(bool), typeof(MainMenuButton), new PropertyMetadata(false));

In another SO post a user said that I can use DataTrigger to react to IsCheckedProperty .在另一篇SO 帖子中,一位用户说我可以使用DataTriggerIsCheckedProperty做出反应。 So I tried the code from the other post but it didn't work:所以我尝试了另一篇文章中的代码,但没有奏效:

<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: :)感谢您的任何答案::)

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! 无论如何,感谢所有回答者的努力!

I guess in your style you haven't specified the correct TargetType . 我猜你的风格没有指定正确的TargetType This should work - 这应该工作 -

<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 local should be added in xaml which corresponds to the namespace where your MainMenuButton class resides. 应该在xaml中添加名称空间local ,它对应于MainMenuButton类所在的命名空间。

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

我认为您不需要绑定到您的DP,尝试使用Property =“IsChecked”作为IsMouseOver。

If anyone is still looking for answers to this, I came across this and tried using如果有人仍在寻找答案,我遇到了这个并尝试使用

<MultiTrigger>
    <MultiTrigger.Conditions>
        <Condition Binding="{...}" Value="..."/>
    </MultiTrigger.Conditions>
</MultiTrigger>

as some answers sugggest but WPF throws an exception that 'Property' must not be null for MultiTrigger.正如一些答案所暗示的那样,但是 WPF 会引发异常,即 MultiTrigger 的“属性”不能是 null。 The solution if you want to use bindings in the trigger conditions is to use Multi Data Trigger.如果要在触发条件中使用绑定,解决方案是使用多数据触发器。

<MultiDataTrigger>
    <MultiDataTrigger.Conditions>
        <Condition Binding="{...}" Value="..."/>
    </MultiDataTrigger.Conditions>
</MultiDataTrigger>

MultiTrigger works with 'Property' while MultiDataTrigger works with bindings. MultiTrigger 与“属性”一起使用,而 MultiDataTrigger 与绑定一起使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM