UWP风格触发失踪 [英] uwp style trigger missing

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

问题描述

似乎UWP XAML不支持在样式触发器。请告诉我常见的解决方法来完成触发类似下面的

it seems that uwp xaml doesn't support triggers in styles. Whats 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 seems to be wrong if I use them not to adjust the controls to the screen. Thanks in advice.

我想我找到执行触发器的正确​​方法在一般为控件。
见下面的代码演示:

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的解决方案。我将与AncestoryType WPF中这样做了,但多数民众赞成UWP太缺少:)无论如何 - 似乎你不能使用核心:DataTriggerBehavior 在风格

推荐答案

在WinRT中,的 RelativeSourceMode 唯一支持的 TemplatedParent 模式, FindAncestor 不可用。因此,当您使用 XAML行为,你需要使用的ElementName 作为一种变通方法。如果你是在项目中使用的DataContext或视图模型,可以绑定到DataContext或视图模型,以避免使用的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>

和视图模型上面使用的:

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天全站免登陆