WinRT リファレンスでEventTriggerを見つけることができましたが、 DataTrigger を見つけることができませんでした。アプリケーションでも使用できませんでした。
WinRT で DataTrigger が本当に欠落していることを確認できる人はいますか? WinRT で使用できるトリガーは EventTrigger だけですか?
WinRT リファレンスでEventTriggerを見つけることができましたが、 DataTrigger を見つけることができませんでした。アプリケーションでも使用できませんでした。
WinRT で DataTrigger が本当に欠落していることを確認できる人はいますか? WinRT で使用できるトリガーは EventTrigger だけですか?
DataTrigger は現在、WinRT XAML ではサポートされていません。
マイク・ブラウンによる補遺
DataTrigger API はVisualStateManagerに置き換えられました。データ トリガーと同様の API は、Blend SDK for Silverlight によって提供されました。Attached Behavior Pattern は 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 は既にビューモデル オブジェクトに設定されています):
<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); }
}
object.Triggersの代わりに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>