2

我正在尝试将旧的 Window phone 7.5 Silverlight 应用程序转换为新的 WinRT Universal 应用程序,但这段代码有问题:

<Style TargetType="Button">
    <Setter Property="Visibility" Value="Collapsed"/>
    <Style.Triggers>
        <DataTrigger Binding="{Binding Active}" Value="True">
            <Setter Property="Visibility" Value="Visible"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

我使用 DataTrigger 根据绑定值设置控件的可见性。

在 Windows Phone 8.1 winrt 应用程序中,此功能已失效。我已经尝试使用 VisualStates 来实现相同的功能,但我无法弄清楚。任何人都可以帮助我或以良好的榜样指导我。我被困在这里...

4

1 回答 1

8

DataTriggers are not available currently in WinRT, you have couple of options instead:

  • use VisualStateManager,
  • use Behaviours managed API, for example like this:

    <Button xmlns:i="using:Microsoft.Xaml.Interactivity"
            xmlns:ic="using:Microsoft.Xaml.Interactions.Core">
        <Button.Style>
            <Style TargetType="Button">
                <Setter Property="Visibility" Value="Collapsed"/>
            </Style>
        </Button.Style>
        <i:Interaction.Behaviors>
            <ic:DataTriggerBehavior Binding="{Binding Active}" Value="True" ComparisonCondition="Equal">
                <ic:ChangePropertyAction PropertyName="Visibility" Value="Visible"/>
            </ic:DataTriggerBehavior>
        </i:Interaction.Behaviors>
    </Button>
    
  • or you can just use binding with apropriate converter:

    <Button Visibility="{Binding Active, Converter={StaticResource BoolToVisibility}}"/>
    
于 2014-12-15T10:40:54.010 回答