Storyboard中的TemplateBinding | ObjectAnimationUsingKeyFrames不起作用

时间:2017-03-07 07:28:08

标签: c# wpf xaml visualstatemanager visualstates

我有以下设置。问题是,如果我切换到VisualState"警报",则AlarmBrush未设置。我尝试了不同的" Bindings" (见下文),但我只收到System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=AlarmBrush; DataItem=null; target element is 'DiscreteObjectKeyFrame' (HashCode=39320280); target property is 'Value' (type 'Object')等错误。

如果我用ResourceKey替换值,那么一切都很好。但我想让这个状态变得动态,所以静态资源不是唯一的。

BaseClass的

[TemplateVisualState(GroupName = "FooGroup", Name = "Alarm")]
public abstract class BaseClass : Control
{
    public static readonly DependencyProperty AlarmBrushProperty = DependencyProperty.Register("AlarmBrush", typeof(Brush), typeof(BaseClass), null);
    public Brush AlarmBrush
    {
        get { return (Brush)GetValue(AlarmBrushProperty); }
        set { SetValue(AlarmBrushProperty, value); }
    }
}

DerivedClass

public class DerivedClass : BaseClass
{
    public DerivedClass()
    {
        this.DefaultStyleKey = typeof(DerivedClass);
    }

    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();
        VisualStateManager.GoToState(this, "Alarm", true);
    }
}

XAML Style

<Style TargetType="Basic:DerivedClass">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Basic:DerivedClass">
                <Grid>
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="FooGroup">
                            <VisualState x:Name="Alarm">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background)" Storyboard.TargetName="AlarmBorder">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{Binding AlarmBrush, RelativeSource={RelativeSource TemplatedParent}}"/>
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type Basic:DerivedClass}}, Path=AlarmBrush}"/>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <Border x:Name="AlarmBorder" BorderBrush="{TemplateBinding BorderBrush}" Padding="{TemplateBinding Padding}"  BorderThickness="{TemplateBinding BorderThickness}">
                        <TextBlock Text="test" Foreground="{TemplateBinding Foreground}" TextWrapping="Wrap" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </Border>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="BorderThickness" Value="5"/>
</Style>

XAML

<Foo:DerivedClass BorderBrush="#FFDA1F1F" AlarmBrush="#FF00FF0C"/>

1 个答案:

答案 0 :(得分:2)

你会讨厌这个,但我记得这是WPF的局限之一。 Value属性需要冻结,这意味着您无法使用绑定动态更改它。同样的事情是To的{​​{1}}属性。

由于您实际上不是动画颜色(动画ColorAnimation只会立即改变颜色),您始终可以获得Brush的参考并手动设置它的AlarmBorder