我有一个带边框的 UserControl,边框的颜色应该使用依赖属性设置。我还想为边界的不透明度设置动画。我当前的 xaml 代码如下所示:
<Border BorderBrush="{Binding ElementName=ImageViewerUserControl,
Path=NotificationColor}" BorderThickness="3" x:Name="AnimatedBorderBrush"
Visibility="{Binding ElementName=ImageViewerUserControl,
Path=ShowSequenceErrorNotification, Converter={StaticResource boolToVisibility}}">
<Border.Triggers>
<EventTrigger RoutedEvent="Border.Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="AnimatedBorderBrush"
Storyboard.TargetProperty="BorderBrush.Opacity"
RepeatBehavior="Forever"
AutoReverse="True"
From="1"
To="0.0"
Duration="0:0:1"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Border.Triggers>
</Border>
Run Code Online (Sandbox Code Playgroud)
这只会给出错误:
无法解析属性路径“BorderBrush.Opacity”中的所有属性引用。验证适用对象是否支持这些属性。
但是,如果我将 BorderBrush 的颜色更改为,可以说它Black有效。这怎么可能实现?我想通过依赖属性设置边框的 Brush 颜色。是的,依赖属性是Brush
我认为这里的问题是,只有存在要设置动画的对象(画笔)时,动画才会起作用。如果您注册时DependencyProperty没有默认值,则默认为空。请尝试使用默认值注册 DP
public static readonly DependencyProperty NotificationColorProperty = DependencyProperty.Register(
"NotificationColor",
typeof(Brush),
typeof(ImageViewerUserControl),
new PropertyMetadata(Brushes.Transparent)
);
Run Code Online (Sandbox Code Playgroud)
编辑:
正如@Sheridan 所说,使用Storyboard.TargetProperty="Opacity"而不是Border.Opacity. 虽然如果您指定直接BorderBrush它就可以工作,但它对于有界 DP 的我来说不起作用。
| 归档时间: |
|
| 查看次数: |
6004 次 |
| 最近记录: |