DataTrigger with VisualStates in Windows phone 8.1

夙愿已清 提交于 2019-12-14 01:55:01

问题


I'm trying to convert old Window phone 7.5 Silverlight Application to new WinRT Universal application and I have problems with this pice of code:

<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>

I used DataTrigger to set visibility of control based on binding value.

In Windows Phone 8.1 winrt app this functionality is out. I've tried with VisualStates to achieve same functionality but I can't figure it out. Can anyone help me or direct me with good example. I'm stuck here with this...


回答1:


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}}"/>
    


来源:https://stackoverflow.com/questions/27481555/datatrigger-with-visualstates-in-windows-phone-8-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!