5

I have a UserControl with a Border, the color of the border should be setted with a Dependency Property. I also want to animate the opacity of the border. My current xaml code looks like this:

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

This only give the error:

Cannot resolve all property references in the property path 'BorderBrush.Opacity'. Verify that applicable objects support the properties.

But if I change to color of the BorderBrush to, lets say Black it works. How is this possible to achieve? I want to set the Brush color of my border via a dependency property. And yes, the dependency property is a Brush

2
  • 1
    Have you set a default Brush for your DependencyProperty? If not default value is null and will cause such an error.
    – LPL
    Oct 8, 2013 at 12:20
  • @LPL How can I achieve this? Oct 8, 2013 at 12:21

2 Answers 2

2

I think the problem here is that the animation will only work if there is an object (the Brush) to animate. If you register your DependencyProperty without a default value it is null by default. Please try registering the DP with a default value

public static readonly DependencyProperty NotificationColorProperty = DependencyProperty.Register(
    "NotificationColor",
    typeof(Brush),
    typeof(ImageViewerUserControl),
    new PropertyMetadata(Brushes.Transparent)
);

Edit:

And as @Sheridan says use Storyboard.TargetProperty="Opacity" instead of Border.Opacity. Although it works if you specify a direct BorderBrush it doesn't worked for me with a bounded DP.

4
  • Sounds like the solution but: I get this error message: Default value type does not match type of property 'NotificationColor'. Oct 8, 2013 at 12:34
  • And what type is NotificationColor? Brush or Color?
    – LPL
    Oct 8, 2013 at 12:37
  • Thanks for your input, but the same error message pops up during runtime. The property is Brush. Oct 8, 2013 at 12:37
  • Jesus. It was just the namespaces. Oct 8, 2013 at 12:49
2

Your AnimatedBorderBrush name is misleading as it relates to a Border and not a BorderBrush. If you want to animate the Border.Opacity, then use Border.Opacity in the DoubleAnimation instead of BorderBrush.Opacity:

<DoubleAnimation Storyboard.TargetName="AnimatedBorderBrush"
    Storyboard.TargetProperty="Border.Opacity"
    RepeatBehavior="Forever"
    AutoReverse="True"
    From="1"
    To="0.0"
    Duration="0:0:1" />

UPDATE >>>

Ahhhhh, my bad... As the animation is defined inside the Border, there is no need to reference it, just use Opacity:

<DoubleAnimation Storyboard.TargetName="AnimatedBorderBrush"
    Storyboard.TargetProperty="Opacity"
    RepeatBehavior="Forever"
    AutoReverse="True"
    From="1"
    To="0.0"
    Duration="0:0:1" />
3
  • I can see what you mean by the naming convention, I tried what you suggested, but no luck. Oct 8, 2013 at 12:24
  • Your answer was correct, but the problem is that the guy over here also "fixed" my problem..Thanks alot! Oct 8, 2013 at 12:50
  • 1
    Not a problem at all.
    – Sheridan
    Oct 8, 2013 at 12:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.