我有以下设置。问题是,如果我切换到VisualState
“警报”,AlarmBrush
则没有设置。我尝试了不同的“绑定”(见下文),但我只收到类似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 替换该值,那么一切都很好。但是我希望这个状态是动态的,所以静态资源是没有选择的。
基类
[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>
xml
<Foo:DerivedClass BorderBrush="#FFDA1F1F" AlarmBrush="#FF00FF0C"/>