1

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

在 UserControl 中,我需要使用如下交互 DataTrigger。原因是我需要一个MyStory带有绑定关键帧值的情节提要 ( )。(这样做之前在这里讨论过。)

<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();
    }
}

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

1.

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

2.输入“开火!” 在运行时的 UserControl2 文本框中。

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

4

0 回答 0