看起来 UWP XAML 不支持在样式中使用触发器。有什么通用的解决方法可以实现像下面这样的触发器吗?
<Style TargetType="Button">
<Style.Triggers>
<Trigger Property="Visibility" Value="Collapsed">
<Setter Property="Text" Value="" />
</Trigger>
</Style.Triggers>
</Style>
目前我看到在UWP中实现触发器的以下选项:
使用动画或VisualStateTriggers。如果不是将它们用于调整控件以适应屏幕,则两者似乎都不正确。
我认为我找到了一般情况下为控件实现触发器的正确方法。 请参见下面的代码示例:
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
。