5

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

我在 Silverlight 论坛中发现了一个线程,描述了完全相同的问题,其中声称这在 SL4 RTM 中有效:http ://forums.silverlight.net/forums/p/174655/423324.aspx 。但是,当我尝试使用 VS2010 WPF 应用程序中发布的代码时,它不起作用,这意味着颜色不会改变。我能够在 a 中做的唯一绑定VisualState Storyboard是 to 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));
}
4

1 回答 1

1

如果您像这样在Generic.xamlx:Name中指定属性,它将按预期工作:<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将模板应用到控件时及时在代码后面设置属性的绑定:

public override void OnApplyTemplate()
{
    var colorAnimation = (ColorAnimation)Template.FindName("PART_ColorAnimation", this);

    if(colorAnimation == null)
        return;

    var binding = new Binding("HotColor") { Source = this };
    BindingOperations.SetBinding(colorAnimation,
                                 ColorAnimation.ToProperty,
                                 binding);
}

希望有所帮助。

于 2013-07-26T15:03:39.487 回答