开发者

DataTrigger in WinRT?

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 any开发者_Python百科one confirm that DataTrigger is really missing in WinRT? Is EventTrigger the only trigger available in WinRT?


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.


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


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>


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); }
}


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜