为什么交互DataTrigger在设计时不生效?

时间:2013-04-14 04:40:51

标签: wpf user-controls datatrigger

好!我做了一个大版本:))

在UserControl中,我需要使用下面的交互 DataTrigger。原因是我需要一个带有绑定关键帧值的故事板(MyStory)。 (之前已经讨论了here。)

<UserControl x:Class="WpfApplication1.UserControl2"
             ...
             xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
             xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
             xmlns:local="clr-namespace:WpfApplication1">

    <UserControl.Resources>
        <Style x:Key="MyControlStyle" TargetType="UserControl">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate>
                        <Grid>
                            <Grid.Resources>
                                <Storyboard x:Key="MyStory">
                                    <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" Storyboard.TargetName="brdBase">
                                        <SplineColorKeyFrame KeyTime="0:0:1" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:UserControl2}}, Path=SpecialColor}"/>
                                    </ColorAnimationUsingKeyFrames>
                                </Storyboard>
                            </Grid.Resources>

                            <Border x:Name="brdBase" BorderThickness="1" BorderBrush="Gray">
                                <TextBox Text="{Binding SpecialText}"/>
                            </Border>

                            <i:Interaction.Triggers>
                                <ei:DataTrigger Binding="{Binding SpecialText}" Value="Fire!">
                                    <ei:ControlStoryboardAction Storyboard="{StaticResource MyStory}" ControlStoryboardOption="Play"/>
                                </ei:DataTrigger>
                            </i:Interaction.Triggers>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </UserControl.Resources>

    <Grid x:Name="grdRoot" DataContext="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:UserControl2}}}">
        <UserControl Style="{DynamicResource MyControlStyle}"/>
    </Grid>

</UserControl>

C#代码背后:

public partial class UserControl2 : UserControl
{
    #region ________________________________________  SpecialColor

    public Color SpecialColor
    {
        get { return (Color)GetValue(SpecialColorProperty); }
        set { SetValue(SpecialColorProperty, value); }
    }

    public static readonly DependencyProperty SpecialColorProperty =
        DependencyProperty.Register("SpecialColor",
                                    typeof(Color),
                                    typeof(UserControl2),
                                    new FrameworkPropertyMetadata(Colors.Red));
    #endregion


    #region ________________________________________  SpecialText

    public string SpecialText
    {
        get { return (string)GetValue(SpecialTextProperty); }
        set { SetValue(SpecialTextProperty, value); }
    }

    public static readonly DependencyProperty SpecialTextProperty =
        DependencyProperty.Register("SpecialText",
                                    typeof(string),
                                    typeof(UserControl2),
                                    new FrameworkPropertyMetadata(string.Empty));

    #endregion


    public UserControl2()
    {
        InitializeComponent();
    }
}

我希望上面的代码在MyStory设置为&#34; Fire!&#34;之后立即播放SpecialText。为此,我们可以使用以下方法之一:

1

<Grid>
    <local:UserControl2 SpecialText="Fire!"/>
</Grid>

2。打字&#34;消防!&#34;在运行时的UserControl2文本框中。

第一种方式在设计时不会影响GUI。我的应用程序中有一些复杂的UserControls,确实需要解决这个问题。有些DP调用故事板来动态更改UserControl的布局。显然,这些故事板涉及一个或多个绑定的关键帧。所以我必须越来越多地运行我的应用程序来检查它:(

0 个答案:

没有答案