缺少 UWP 样式触发器 [英] UWP style trigger missing

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

问题描述

似乎 UWP XAML 不支持样式中的触发器.完成以下触发器的常见解决方法是什么?

It seems that UWP XAML doesn't support triggers in styles. What is the common workaround to accomplish triggers like the following?

<Style TargetType="Button">
    <Style.Triggers>
        <Trigger Property="Visibility" Value="Collapsed">
            <Setter Property="Text" Value="" />
        </Trigger>
    </Style.Triggers>
</Style>

目前我看到以下选项可以在 UWP 中完成触发器:

At the moment I see the following options to accomplish triggers in UWP:

使用动画或 VisualStateTriggers.如果我使用它们不将控件调整到屏幕上,两者似乎都是错误的.

Use Animations or VisualStateTriggers. Both seem to be wrong if I use them not to adjust the controls to the screen.

我想我找到了为控件实现触发器的正确​​方法.请参阅以下代码作为演示:

I think I found the correct way to implement Triggers in general for Controls. See the below code as demonstration:

xmlns:Interactivity="using:Microsoft.Xaml.Interactivity" 
xmlns:Core="using:Microsoft.Xaml.Interactions.Core"

<Border x:Name="BackgroundElement" Tag="Text">
    <Interactivity:Interaction.Behaviors>
        <Core:DataTriggerBehavior Binding="{Binding Tag, ElementName=BackgroundElement}" Value="Text">
            <Core:ChangePropertyAction PropertyName="BorderBrush" Value="AliceBlue" />
        </Core:DataTriggerBehavior>
    </Interactivity:Interaction.Behaviors>
</Border>

如果有一个没有 ElementName 的解决方案,那就太棒了.我会在 WPF 中使用 AncestorType 完成此操作,但在 UWP 中也没有.反正样式里好像不能用Core:DataTriggerBehavior

It would be awesome if there is a solution without ElementName. I would have done this in WPF with AncestorType, but that's missing in UWP too. Anyway, it seems that you can't use the Core:DataTriggerBehavior in styles.

推荐答案

在 WinRT 中,RelativeSourceMode 仅支持SelfTemplatedParent 模式,FindAncestor 不可用.因此,当您使用 XAML 行为 时,您需要使用 ElementName 作为解决方法.如果您在项目中使用 DataContext 或 ViewModel,则可以绑定到 DataContext 或 ViewModel 以避免使用 ElementName.例如:

In WinRT, RelativeSourceMode only support Self and TemplatedParent mode, FindAncestor is not available. So when you use XAML Behaviors, you need to use ElementName as a workaround. And if you are using DataContext or ViewModel in your project, you can bind to the DataContext or ViewModel to avoid using ElementName. For example:

<Page ...>
    <Page.Resources>
        <local:MyViewModel x:Key="ViewModel" />
    </Page.Resources>
    ...
    <Border x:Name="BackgroundElement" DataContext="{Binding Source={StaticResource ViewModel}}">
        <Interactivity:Interaction.Behaviors>
            <Core:DataTriggerBehavior Binding="{Binding Tag}" Value="Text">
                <Core:ChangePropertyAction PropertyName="Background" Value="Red" />
            </Core:DataTriggerBehavior>
        </Interactivity:Interaction.Behaviors>
    </Border>
    ...
</Page>

以及上面使用的ViewModel:

And the ViewModel used above:

public class MyViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private string _tag;

    public string Tag
    {
        get
        {
            return _tag;
        }

        set
        {
            _tag = value;
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("Tag"));
            }
        }
    }
}

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

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