18

I was able to find EventTrigger in the WinRT reference, however, I wasn't able to find DataTrigger. I wasn't able to use it in an application either.

Can anyone confirm that DataTrigger is really missing in WinRT? Is EventTrigger the only trigger available in WinRT?

Charles
  • 50,943
  • 13
  • 104
  • 142
Murven
  • 2,377
  • 17
  • 23
  • http://msdn.microsoft.com/en-us/library/windows/apps/br211377(v=VS.85).aspx ... and is that such a bad thing that you want to trigger on an event and not on a dataelement? Also it bears repeating that this is not even beta stages... – jcolebrand Sep 16 '11 at 05:44
  • 1
    It isn't a bad or a good thing, I just wanted confirmation so that I didn't invest anymore time down that path. BTW, my question does link to the same reference you point me to. In order to change the state of view I use either DataTrigger or the Blend behaviors like GoToStateAction and neither of those seem to be available at this time, I just wanted to make sure I did have to look for a workaround. – Murven Sep 16 '11 at 20:24
  • DataTriggers were shunned in Silverlight for the [ViewStateManager](http://msdn.microsoft.com/en-us/library/system.windows.visualstatemanager(v=vs.95).aspx) It looks like they took the [same approach in WinRT](http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.visualstatemanager.aspx) – Michael Brown Oct 09 '12 at 13:58
  • 2
    It is an interesting decision, I do not think that the VisualStateManager substitutes data triggers in any way, just that coincidentally, developers were using these triggers to do things associated with the VisualStateManager, however in WPF 4, I make full use of both VisualStateManager *and* data triggers. – Murven Oct 19 '12 at 22:43

5 Answers5

20

DataTrigger is not currently supported in WinRT XAML.

Addendum by Mike Brown

The DataTrigger API has been replaced with the VisualStateManager a similar API to Data Triggers was provided by the Blend SDK for Silverlight. Since the Attached Behavior Pattern works in WinRT, it is possible to do the same.

Michael Brown
  • 9,041
  • 1
  • 28
  • 37
Tim Heuer
  • 4,301
  • 21
  • 20
  • 1
    I do like authoritative answers – jcolebrand Sep 16 '11 at 20:56
  • 7
    I'd say that's a big omission. Is it planned for the final version? – svick Sep 17 '11 at 00:51
  • Remember that WinRT was designed using Silverlight as a jumping-off point. (At BUILD, it was revealed that the WinRT API design team had examined the MSIL of Windows Phone 7 applications submitted to the marketplace, to see which classes/methods were commonly used in real-world apps.) Just as System.Data is missing from both Silverlight and WinRT, so too are triggers. Perhaps some of these WPF features will make their way into **_Windows 9_**, but it seems somewhat unlikely for Windows 8. – Tao Yue Feb 22 '12 at 22:01
  • 5
    I still fail to see how the VisualStateManager represents a replacement for data triggers. They offer different functionality and they can be used in conjunction. Do you have any more details concerning why it was judged that VisualStateManager could work as a substitute for DataTrigger? – Murven Apr 15 '13 at 18:45
3

What about this project that seems implement triggers in WinRT : http://winrttriggers.codeplex.com/

Vincent Ricosti
  • 499
  • 5
  • 8
2

I don't know when it changed but i have DataTriggerBehavior and GoToStateAction combining them should solve your problem...

namespace imports

xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" 
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 
xmlns:Interactivity="using:Microsoft.Xaml.Interactivity" 
xmlns:Core="using:Microsoft.Xaml.Interactions.Core"

ViewSateManager place on root element

<VisualStateManager.VisualStateGroups>
    <VisualStateGroup x:Name="Common">
        <VisualStateGroup.Transitions>
            <VisualTransition GeneratedDuration="0" To="Online">
                <Storyboard>
                    <ColorAnimation Duration="0" To="Lime" Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)" Storyboard.TargetName="Name" />
                </Storyboard>
            </VisualTransition>
            <VisualTransition GeneratedDuration="0" To="Offline">
                <Storyboard>
                    <ColorAnimation Duration="0" To="Red" Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)" Storyboard.TargetName="Name" />
                </Storyboard>
            </VisualTransition>
        </VisualStateGroup.Transitions>
        <VisualState x:Name="Online" />
        <VisualState x:Name="Offline" />
    </VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Interactivity:Interaction.Behaviors>
    <Core:DataTriggerBehavior Binding="{Binding Active}" Value="True">
        <Core:GoToStateAction StateName="Online" />
    </Core:DataTriggerBehavior>
    <Core:DataTriggerBehavior Binding="{Binding Active}" Value="False">
        <Core:GoToStateAction StateName="Offline" />
    </Core:DataTriggerBehavior>
</Interactivity:Interaction.Behaviors>
Peter
  • 37,042
  • 39
  • 142
  • 198
  • can you please add some reference about the Microsoft.Xaml.Interactions.Core namespace, which assembly it is contained in and what product is it part of? Thanks! – Murven Jun 17 '14 at 21:37
  • I have found the reference: Microsoft.Xaml.Interactions.Core namespace is in the Microsoft.Xaml.Interactions.dll assembly which is included in the Behaviors SDK (XAML) for Visual Studio 2013, which is part of Blend for Visual Studio 2013. – Murven Jun 17 '14 at 21:50
0

I implemented an alternate workaround that may work for you. Steps:

  1. Create a UserControl (either from scratch or inheriting) so you can write some code-behind C# into the control.
  2. Create a DependencyProperty in the codebehind for the databinding you want to trigger on.
  3. Use the DependencyProperty's PropertyChangedCallback method to implement do what you need to do in code to the control.
  4. Bind the DependencyProperty in XAML to the data you want to trigger on.

It's not as clean as a DataTrigger, but it's not too much worse and it works well (for me at least).

Declaration in XAML (DataContext is already set to a viewmodel object):

<local:PlayButton IsPlaying="{Binding IsPlaying}"/>

Example DependencyProperty that triggers storyboards to change state:

// Use this to implement storyboard changing in W8 since triggers are not supported
public static readonly DependencyProperty IsPlayingProperty = DependencyProperty.Register(
      "IsPlaying",
      typeof(bool),
      typeof(PlayButton),
      new PropertyMetadata(null,
          new PropertyChangedCallback(OnIsPlayingChanged)
      ));

private static void OnIsPlayingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    PlayButton pb = (PlayButton)d;
    bool isPlaying = (bool)e.NewValue;

    if (isPlaying == false)
        pb.GotoPlay.Begin();
    else
        pb.GotoPause.Begin();
}

public bool IsPlaying
{
    get { return (bool)GetValue(IsPlayingProperty); }
    set { SetValue(IsPlayingProperty, value); }
}
Ray Ackley
  • 390
  • 4
  • 9
  • Yes, I agree this is a good workaround for the lack of DataTriggers. It is simply sad that something which was working well and offered convenience and full Blend support was removed in the technology that is considered to be the future of Windows applications. WPF is still so much better and superior in so many ways to Silverlight and WinRT. – Murven Oct 13 '13 at 01:40
-1

you can use VisualState instead of object.Triggers in Windows 8 Here is the code

<ControlTemplate TargetType="Button">
  <Grid>
    <VisualStateManager.VisualStateGroups>
      <VisualStateGroup x:Name="CommonStates">
        <VisualStateGroup.Transitions>
          <!--Take one half second to transition to the PointerOver state.-->
          <VisualTransition To="PointerOver" GeneratedDuration="0:0:0.5"/>
        </VisualStateGroup.Transitions>

        <VisualState x:Name="Normal" />

        <VisualState x:Name="PointerOver">
          <Storyboard>
            <ColorAnimation Storyboard.TargetName="ButtonBrush"
                            Storyboard.TargetProperty="Color" To="Red" />
          </Storyboard>
        </VisualState>
      </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>
    <Grid.Background>
      <SolidColorBrush x:Name="ButtonBrush" Color="Green"/>
    </Grid.Background>
  </Grid>
</ControlTemplate>
Martin Liversage
  • 104,481
  • 22
  • 209
  • 256
Ibraheem Osama
  • 324
  • 3
  • 6
  • 3
    Can you please explain why you think this works as a replacement for DataTriggers? I do not see anything in your example that would let me create a binding to data in the view model and changes on that binding would produce changes in the UI. – Murven Apr 15 '13 at 18:48