gpt4 book ai didi

c# - Storyboard中的模板绑定(bind) | ObjectAnimationUsingKeyFrames 不工作

转载 作者:行者123 更新时间:2023-11-30 16:45:12 25 4
gpt4 key购买 nike

我有以下设置。问题是,如果我切换到 VisualState“Alarm”,则未设置 AlarmBrush。我尝试了不同的“绑定(bind)”(见下文),但我只收到类似 System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element 的错误。 BindingExpression:Path=AlarmBrush;数据项=空;目标元素是 'DiscreteObjectKeyFrame' (HashCode=39320280);目标属性是“值”(类型“对象”)

如果我用 ResourceKey 替换该值,那么一切都很好。但是我想让这个 State 动态,所以静态资源不是 opionion。

基类

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

派生类

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

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

XAML 样式

<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"/>

最佳答案

你会讨厌这个,但我记得这是 WPF 的限制之一。 Value 属性需要被卡住,这意味着您不能使用绑定(bind)来动态更改它。 ColorAnimationTo 属性也是如此。

因为您实际上并没有动画颜色(动画Brush只会立即改变颜色),您总是可以获得AlarmBorder<的引用 并手动设置其 Background

关于c# - Storyboard中的模板绑定(bind) | ObjectAnimationUsingKeyFrames 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42642574/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com