我已经设置了一个非常简单的原型。使用Blend 4,我设置了一个名为'Quote'的简单示例数据源,它具有两个属性P1和P2的Collection。 P1是4位数字,P2是字符串。还有另外两个简单的属性,SecurityType和Price。这两个属性都在Quote。
之下我正在使用i:Interaction.Triggers和DataTriggers与GoToStateAction。如果我在Silverlight项目中运行它们,这些工作正常。在WPF中,DataTriggers不起作用。 我错过了什么? WPF中没有编译或运行时错误。如果我在我定义的DataTemplate中使用WPF DataTriggers,它工作正常,但我需要与SL和WPF交叉兼容的XAML。
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="WpfApplication9.MainWindow"
x:Name="Window"
Title="MainWindow"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:SampleData="clr-namespace:Expression.Blend.SampleData.Quote"
mc:Ignorable="d"
Width="640" Height="480">
<Window.Resources>
<SampleData:Quote x:Key="Quote" d:IsDataSource="True"/>
<DataTemplate x:Key="FieldListBaseItemTemplate" >
<Grid x:Name="Fields" Height="25" Background="Gray" Tag="Yes">
<VisualStateManager.CustomVisualStateManager>
<ei:ExtendedVisualStateManager/>
</VisualStateManager.CustomVisualStateManager>
<i:Interaction.Triggers>
<ei:DataTrigger Binding="{Binding P2}" Value="ATT">
<ei:GoToStateAction TargetObject="{Binding ElementName=Fields}" StateName="SimpleFieldState" />
</ei:DataTrigger>
<ei:DataTrigger Binding="{Binding P2}" Value="HL" >
<ei:GoToStateAction TargetObject="{Binding ElementName=Fields}" StateName="OtherFieldState" />
</ei:DataTrigger>
<ei:DataTrigger Binding="{Binding P2}" Value="Ford" >
<ei:GoToStateAction TargetObject="{Binding ElementName=Fields}" StateName="OtherFieldState" />
</ei:DataTrigger>
</i:Interaction.Triggers>
<!--
<ei:DataStateBehavior Binding="{Binding P2}" Value="ATT" TrueState="SimpleFieldState" FalseState="None" />
<ei:DataStateBehavior Binding="{Binding P2}" Value="Ford" TrueState="OtherFieldState" FalseState="None"/>
<ei:DataStateBehavior Binding="{Binding P2}" Value="HL" TrueState="OtherFieldState" FalseState="None"/>
<i:Interaction.Behaviors>
<ei:DataStateBehavior Binding="{Binding P2}" Value="ATT" TrueState="SimpleFieldState" FalseState="None"/>
</i:Interaction.Behaviors>
-->
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="FieldsListStateGroup">
<VisualState x:Name="None"/>
<VisualState x:Name="SimpleFieldState">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="FieldLabel">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<Visibility>Visible</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="FieldValue">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<Visibility>Visible</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(TextBlock.Text)" Storyboard.TargetName="FieldLabel">
<DiscreteObjectKeyFrame KeyTime="0" Value="30-Day Yield"/>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(TextBlock.Text)" Storyboard.TargetName="FieldValue">
<DiscreteObjectKeyFrame KeyTime="0" Value="23.34"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="OtherFieldState">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="FieldLabel">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<Visibility>Visible</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="FieldValue">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<Visibility>Visible</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(TextBlock.Text)" Storyboard.TargetName="FieldLabel">
<DiscreteObjectKeyFrame KeyTime="0" Value="Close Price"/>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(TextBlock.Text)" Storyboard.TargetName="FieldValue">
<DiscreteObjectKeyFrame KeyTime="0" >
<DiscreteObjectKeyFrame.Value>
<Binding ElementName="LayoutRoot" Path="DataContext.SecurityType" FallbackValue="Failed to Bind" />
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="125"/>
<ColumnDefinition Width="125"/>
</Grid.ColumnDefinitions>
<Border Grid.Column="0" Margin="0,-1,0,1" Width="85" Background="Black">
<TextBlock x:Name="FieldLabel" Foreground="White"/>
</Border>
<Border x:Name="border" Grid.Column="1" Width="Auto" Background="Black" >
<TextBlock x:Name="FieldValue" Foreground="White" />
</Border>
</Grid>
</DataTemplate>
</Window.Resources>
<Grid x:Name="LayoutRoot" Background="White" DataContext="{Binding Source={StaticResource Quote}}">
<Grid.RowDefinitions>
<RowDefinition /><RowDefinition />
</Grid.RowDefinitions>
<TextBlock x:Name="T" Text="{Binding SecurityType}" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" Height="14" Width="54" Margin="0" LineHeight="14" LineStackingStrategy="BlockLineHeight"/>
<ListBox MaxHeight="355" Background="AntiqueWhite" Grid.Row="1" Margin="0,0,1,0" IsSynchronizedWithCurrentItem="False" ItemsSource="{Binding Collection}" ItemTemplate="{DynamicResource FieldListBaseItemTemplate}"/>
</Grid>
</Window>