Использование привязки в VisualState Storyboard - PullRequest
5 голосов
/ 02 декабря 2010

Я пишу пользовательский элемент управления для приложения WPF. Я хочу использовать цветную анимацию в Storyboard в VisualState определении. Свойство To этой анимации должно быть связано со свойством зависимостей моего объекта управления. Это не похоже на работу.

Я нашел тему на форуме Silverlight, описывающую точно такую ​​же проблему, в которой утверждается, что это работает в SL4 RTM: http://forums.silverlight.net/forums/p/174655/423324.aspx. Однако, когда я пытаюсь использовать код, размещенный в моем приложении WP201 VS2010, он не работает, то есть цвет не меняется. Единственное связывание, которое я смог сделать в 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 Ответ

1 голос
/ 26 июля 2013

Будет работать должным образом, если вы укажете атрибут x:Name для <ColorAnimation> в Generic.xaml следующим образом:

<!-- This WILL work: -->
<ColorAnimation x:Name="PART_ColorAnimation"
                Storyboard.TargetProperty="Background.Color"
                Storyboard.TargetName="MyBorder"
                Duration="0:0:0.2" />

и установите привязку для свойства To позже в коде, который отстает во времени, когда шаблон будет уже применен к элементу управления путем переопределения OnApplyTemplate () в TestCustomControl.cs :

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

Надеюсь, что поможет.

...