2

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

freshbm
  • 5,540
  • 5
  • 46
  • 75

1 Answers1

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}}"/>
    
Community
  • 1
  • 1
Romasz
  • 29,662
  • 13
  • 79
  • 154
  • thank you, been searching for a Solution for W10 for minutes :) – Daniel Hitzel Sep 22 '15 at 19:28
  • @Romasz This works great when navigating to a page, but the DataTriggerBehavior does not appear to be triggered when navigating back to that page. I'm changing my ViewModel after the navigation occurred but somehow it's not being triggered. Any ideas why? – Thierry Jan 21 '16 at 02:30
  • @Thierry I'm not sure - it's hard to say without the code. I suppose you already have debugged the app. In this case, maybe you can ask new question on SO and put some code there. – Romasz Jan 21 '16 at 05:27
  • @Romasz I have debugged it & it's still not sorted! I've replaced the DataTriggerBehaviors by a class which contains a dependency property and I've binded this property to the GoToState and the same problem occurs! Spent hours on this and will continue to work on it until I find a solution or work-around, but just can't put my finger on what's wrong considering that I can clearly see my VM's prop changing its value and I can see it changing the view state when resizing but it's just triggering the wrong one when "going" back to the previous page. Not even sure triggering is the right term. – Thierry Jan 21 '16 at 15:10