在VisualState Storyboard中使用绑定

时间:2010-12-02 11:46:05

标签: wpf binding storyboard visualstatemanager

我正在为WPF应用程序编写自定义控件。我想在Storyboard定义中的VisualState中使用颜色动画。该动画的To属性应绑定到我的控件对象的依赖项属性。这似乎不起作用。

我在Silverlight论坛中找到了一个线程,描述了完全相同的问题,声称这可以在SL4 RTM中运行:http://forums.silverlight.net/forums/p/174655/423324.aspx。 但是,当我尝试使用我的VS2010 WPF应用程序中发布的代码时,它不起作用,这意味着颜色不会改变。我在VisualState Storyboard中唯一可以做的绑定是StaticResource

有什么想法吗?

编辑:

添加了代码段:

来自Generic.xaml的

<Style TargetType="{x:Type local:TestCustomControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:TestCustomControl}">
                <Border BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}"
                        Name="MyBorder">
                    <Border.Background>
                        <SolidColorBrush Color="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ColdColor}" />
                    </Border.Background>

                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Normal"/>
                            <VisualState x:Name="MouseOver">
                                <Storyboard>
                                    <!-- This works: -->
                                    <!--<ColorAnimation Storyboard.TargetProperty="Background.Color" Storyboard.TargetName="MyBorder" To="Red"  Duration="0:0:0.2"/>-->

                                    <!-- This also works: --> 
                                    <!--<ColorAnimation Storyboard.TargetProperty="Background.Color" Storyboard.TargetName="MyBorder" To="{StaticResource HotColorRes}"  Duration="0:0:0.2"/>-->

                                    <!-- This doesn't work: -->
                                    <ColorAnimation Storyboard.TargetProperty="Background.Color" Storyboard.TargetName="MyBorder" To="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=HotColor}"  Duration="0:0:0.2"/>

                                </Storyboard>
                            </VisualState>

                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

TestCustomControl.cs:

public class TestCustomControl : Button
{
    static TestCustomControl()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(TestCustomControl), new FrameworkPropertyMetadata(typeof(TestCustomControl)));
    }

    public Color HotColor
    {
        get { return (Color)GetValue(HotColorProperty); }
        set { SetValue(HotColorProperty, value); }
    }

    // Using a DependencyProperty as the backing store for HotColor.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty HotColorProperty =
        DependencyProperty.Register("HotColor", typeof(Color), typeof(TestCustomControl), new UIPropertyMetadata(Colors.Aqua));

    public Color ColdColor
    {
        get { return (Color)GetValue(ColdColorProperty); }
        set { SetValue(ColdColorProperty, value); }
    }

    // Using a DependencyProperty as the backing store for ColdColor.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty ColdColorProperty =
        DependencyProperty.Register("ColdColor", typeof(Color), typeof(TestCustomControl), new UIPropertyMetadata(Colors.Aqua));
}

1 个答案:

答案 0 :(得分:1)

如果您在 Generic.xaml 中指定x:Name的{​​{1}}属性,它将按预期工作:

<ColorAnimation>

并稍后在代码中设置<!-- This WILL work: --> <ColorAnimation x:Name="PART_ColorAnimation" Storyboard.TargetProperty="Background.Color" Storyboard.TargetName="MyBorder" Duration="0:0:0.2" /> 属性的绑定,当模板已经应用于控制时,通过覆盖 TestCustomControl.cs 中的OnApplyTemplate()

To

希望有所帮助。