我能在WinRT参考资料中找到EventTrigger,但是我找不到DataTrigger。我也无法在应用程序中使用它。
有人能确认在WinRT中确实缺失了DataTrigger吗?EventTrigger是WinRT中唯一可用的触发器吗?
我能在WinRT参考资料中找到EventTrigger,但是我找不到DataTrigger。我也无法在应用程序中使用它。
有人能确认在WinRT中确实缺失了DataTrigger吗?EventTrigger是WinRT中唯一可用的触发器吗?
目前WinRT XAML不支持DataTrigger。
Mike Brown的补充说明
DataTrigger API已被VisualStateManager替换,Blend SDK为Silverlight提供了类似于Data Triggers的API。由于附加行为模式在WinRT中有效,因此可以实现相同的功能。
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已设置为视图模型对象):
<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>