Search code examples
winrt-xamlwindows-phone-8.1datatriggervisualstates

DataTrigger with VisualStates in Windows phone 8.1


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


Solution

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