我能够在 WinRT 参考中找到EventTrigger,但是,我无法找到 DataTrigger。我也无法在应用程序中使用它。
谁能确认 WinRT 中确实缺少 DataTrigger?EventTrigger 是 WinRT 中唯一可用的触发器吗?
我能够在 WinRT 参考中找到EventTrigger,但是,我无法找到 DataTrigger。我也无法在应用程序中使用它。
谁能确认 WinRT 中确实缺少 DataTrigger?EventTrigger 是 WinRT 中唯一可用的触发器吗?
WinRT XAML 目前不支持 DataTrigger。
麦克布朗的附录
DataTrigger API 已被VisualStateManager替换,与数据触发器类似的 API 由 Silverlight 的 Blend SDK 提供。由于附加行为模式在 WinRT 中有效,因此可以这样做。
这个似乎在 WinRT 中实现触发器的项目怎么样:http ://winrttriggers.codeplex.com/
我不知道它什么时候改变了,但我有DataTriggerBehavior
并且GoToStateAction
将它们结合起来应该可以解决你的问题......
命名空间导入
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 放置在根元素上
<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>
我实施了一种可能对您有用的替代解决方法。脚步:
它不如 DataTrigger 干净,但也不会差太多,而且效果很好(至少对我来说)。
XAML 中的声明(DataContext 已设置为 viewmodel 对象):
<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); }
}
您可以在 Windows 8 中使用 VisualState 而不是 object.Triggers 这是代码
<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>