WinRT 中的数据触发器?

发布于 2024-12-05 06:33:41 字数 258 浏览 2 评论 0原文

我能够找到 EventTrigger 然而,在 WinRT 参考中,我找不到 DataTrigger。我也无法在应用程序中使用它。

谁能确认 WinRT 中确实缺少 DataTrigger? EventTrigger 是 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 anyone confirm that DataTrigger is really missing in WinRT? Is EventTrigger the only trigger available in WinRT?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

怀中猫帐中妖 2024-12-12 06:33:41

WinRT XAML 当前不支持 DataTrigger。

Mike Brown 的附录

DataTrigger API 已替换为 VisualStateManager 与数据触发器类似的 API 由 Blend SDK for Silverlight 提供。由于附加行为模式在 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.

开始看清了 2024-12-12 06:33:41

我不知道它何时更改,但我有datatriggerbehaviorgotostateaction组合它们应该解决您的问题...

namespace inmanspace imports 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 eviewatemanager plot on root元素上的位置

<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 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>
人心善变 2024-12-12 06:33:41

这个似乎在 WinRT 中实现触发器的项目怎么样: http://winrttriggers.codeplex.com/

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

灼痛 2024-12-12 06:33:41

我实施了一种可能适合您的替代解决方法。步骤:

  1. 创建一个 UserControl(从头开始或继承),以便您可以将一些 C# 代码隐藏到该控件中。
  2. 在代码隐藏中为要触发的数据绑定创建 DependencyProperty。
  3. 使用 DependencyProperty 的 PropertyChangedCallback 方法来实现您需要在控件代码中执行的操作。
  4. 将 XAML 中的 DependencyProperty 绑定到要触发的数据。

它不像 DataTrigger 那样干净,但也没有差太多,而且效果很好(至少对我来说)。

XAML 中的声明(DataContext 已设置为视图模型对象):

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

触发故事板更改状态的示例 DependencyProperty:

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

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); }
}
素手挽清风 2024-12-12 06:33:41

您可以使用Visualstate而不是对象。Windows8中的触发者是代码

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文