混合触发器与WPF触发器 [英] Blend Trigger vs. WPF Trigger

查看:143
本文介绍了混合触发器与WPF触发器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我看来,在Interactivity命名空间中找到的Blend-style Trigger与通过WPF中的Styles,ControlTemplate等可用的经典Trigger之间有很大的区别(I认为这可能也适用于SilverLight).

It seems to me that there is a major difference between the Blend-style Triggers found in the Interactivity namespace, and the classic Triggers available via Styles, ControlTemplates, etc, in WPF (I supposed this probably applies to SilverLight as well).

在WPF中,当您使用设置器设置Trigger时,会得到两种行为:如果满足触发条件,则应用设置器.但是,一旦不再满足触发器条件,将恢复先前的值.在对UI进行编程时,这非常有用.

In WPF, when you set a Trigger with a Setter, you get two behaviours: if the trigger condition is met, the Setter is applied. However, once a Trigger is no longer satisfied, the previous value is restored. This is tremendously useful when programming UI.

我尝试使用Blend方法类似地编写DataTrigger的代码:我将ChangePropertyAction应用于控件,并使用对ViewModel的绑定使其触发了交互性DataTrigger.

I've tried to code a DataTrigger similarly using the Blend way: I applied ChangePropertyAction to my control, and have it trigger off of Interactivity DataTrigger using a Binding to my ViewModel.

<Rectangle x:Name="SecondaryHighlightBackground" Fill="#FF505050">
            <i:Interaction.Triggers>
                <ei:DataTrigger Binding="{Binding IsHighlighted}" Value="Value">
                    <ei:ChangePropertyAction PropertyName="Opacity" Value="0.5"/>
                </ei:DataTrigger>
            </i:Interaction.Triggers>
...
</Rectangle>

所以这按预期工作了……除了我惊讶地发现,当我将IsHighlighted值翻转为false时,原来的不透明度0%并未恢复(我将其降低了).为了再次确认这一点,我编写了以下示例进行验证:

So this worked as expected... except I was stunned to discover that when I flip the IsHighlighted value to false, that the original Opacity of 0% is not restored (I set that lower down). To double check this, I wrote this example to verify:

<Window x:Class="TestWpfApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Name="Window"
        Title="MainWindow"
        Width="640"
        Height="480">

    <Grid x:Name="LayoutRoot">
        <Button Name="button1"
                Width="173"
                Height="57"
                Margin="10,10,0,0"
                HorizontalAlignment="Left"
                VerticalAlignment="Top">
            <Button.Style>
                <Style TargetType="Button">
                    <Setter Property="Content" Value="foo" />
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding IsMouseOver, ElementName=button1}" Value="True">
                            <Setter Property="Content" Value="bar" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Button.Style>
        </Button>
    </Grid>
</Window>

预期的行为是,如果将鼠标悬停在按钮上,则内容将变为"bar".移开鼠标可将内容恢复为样式"所指定的"Foo".样式设置Content以避免出现属性优先级问题.

The expected behaviour is that if you if mouse over the button, the content changes to 'bar'. Moving the mouse away restores content to 'Foo', as specified by the Style. The style sets the Content to avoid the property precedence issue.

我的问题是: Blend扩展中是否确实缺少双向触发,还是有某种方式可以启用此功能?

我错过了什么吗?我认为这就是触发器的关键所在,因为它们具有双向功能.我想继续使用Blend交互操作,因为我发现它们很有用且直观,但是这种事情迫使我不得不重新手工编写XAML.

Am I missing something? I thought that was the very point of triggers, that they have this two-way functionality. I would like to continue using the Blend interactivity actions because I find them useful and intuitive, but this kind of thing forces me back into coding XAML by hand.

推荐答案

如其他答案所述,似乎没有自动反转由Interaction触发器设置的值,但是您可以显式取消设置由Interaction触发器设置的值.使用DependencyProperty.UnsetValue触发,因此将依赖项属性的当前值恢复为以前的值,如下所示:

As stated in other answers there seems to be no automatic reversal of the value set by an Interaction trigger, but you can explicitly unset the value set by the trigger using DependencyProperty.UnsetValue and hence have the dependency property current value revert to the previous value like this:

<i:Interaction.Triggers>
  <ei:DataTrigger Binding="{Binding IsHighlighted}" Value="True">
    <ei:ChangePropertyAction PropertyName="Opacity" Value="0.5"/>
  </ei:DataTrigger>
  <ei:DataTrigger Binding="{Binding IsHighlighted}" Value="False">
    <ei:ChangePropertyAction PropertyName="Opacity" 
                             Value="{x:Static DependencyProperty.UnsetValue}"/>
  </ei:DataTrigger>
</i:Interaction.Triggers>

回复旧帖子,但将其放置在此处,以便其他人(例如我)查找触发器的信息.

Replying to an old post, but putting this up here for others like me looking for info on triggers.

免责声明:我实际上仅使用新的 xaml行为的开源包装.保留经典的i/ei名称空间以与原始问题保持一致.

Disclaimer: I have actually only tested this on .NET Core 3.1 using the new open source packaging of xaml behaviors. Kept the classic i/ei namespaces for consistency with the original question.

这篇关于混合触发器与WPF触发器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆