1

I have the following property definition:

public static class Extensions
{
    public static readonly DependencyProperty TestProperty = DependencyProperty.RegisterAttached("Test", typeof(Storyboard), typeof(UIElement), new UIPropertyMetadata(null));

    public static void SetTest(UIElement element, Storyboard value)
    {
         element.SetValue(TestProperty, value);
    }

    public static Storyboard GetTest(UIElement element)
    {
         return (Storyboard)element.GetValue(TestProperty);
    }
}

And try it use in the xaml

<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:Application;assembly=Application"
    x:Name="template"
    Height="40" Width="1460">
    <Grid Background="#FF000000">
        <TextBlock Foreground="White" VerticalAlignment="Center" HorizontalAlignment="Left" TextAlignment="Left" FontSize="32">Example</TextBlock>
    </Grid>
    <UserControl.Style>
        <Style>
            <Setter Property="local:Extensions.Test">
                <Setter.Value>
                    <Storyboard>
                        <DoubleAnimation Storyboard.TargetName="template" From="270" To="360" Duration="0:0:1" Storyboard.TargetProperty="Angle" />
                    </Storyboard>
                </Setter.Value>
            </Setter>
         </Style>
    </UserControl.Style>
</UserControl>

when i try to load such xaml (for example using XamlReader.Load(stream);), i get the exception with the following message

'local:Extensions.Test' property is not a valid DependencyProperty on the type 'Application.Extensions'. Verify that the property has a DependencyProperty defined with a member field that ends with the 'Property' suffix. Error at object 'System.Windows.Setter'.

Can anybody help me with this problem?

thanks in advance!

basilkot
  • 592
  • 6
  • 15

1 Answers1

3

The owner type should be Extensions instead of UIElement:

public static readonly DependencyProperty TestProperty =
    DependencyProperty.RegisterAttached(
        "Test",
        typeof(Storyboard),
        typeof(Extensions), // here
        new UIPropertyMetadata(null)); 

Could also be written without metadata:

public static readonly DependencyProperty TestProperty =
    DependencyProperty.RegisterAttached(
        "Test",
        typeof(Storyboard),
        typeof(Extensions)); 
Clemens
  • 123,504
  • 12
  • 155
  • 268
  • No, he doesn't want this to be a DependencyProperty of instances of the class "Extensions", he wants this to be an Attached Property for instances of the existing class `UIElement`, from which `UserControl` inherits. – Alain Apr 03 '12 at 17:54
  • @Alain you're wrong. The owner type has to be the type that declares the property. You can not declare another type to be the owner of your attached property. Read [here](http://msdn.microsoft.com/en-us/library/ms597495.aspx) and try before you downvote. – Clemens Apr 03 '12 at 18:00
  • If that is true solution is obviously not to make the owner "Extensions", or he wouldn't be able to apply the property to UIElements, which is clearly his intention. However, it should also not be necessary for him to create a new type that inherits from UIElement. – Alain Apr 03 '12 at 18:05
  • @Alain Also wrong. Simply try it. Canvas.Left is defined in Canvas, but can be applied to TextBlock. Owner type **must** be the type that declares the property. And the owner type of an attached property must not even be a DependencyObject. – Clemens Apr 03 '12 at 18:06
  • @Clemens, third parameter (OwnerType) means Type, where this attached property will use, not type where this property is declared. – basilkot Apr 03 '12 at 18:10
  • @basilkot That's not true, and maybe a common misunderstanding. Why don't you simply try it? – Clemens Apr 03 '12 at 18:11
  • @Clemens, but you are right) it solves my problem with XamlRead, but this property always is null – basilkot Apr 03 '12 at 18:16
  • @basilkot maybe Storyboard can't be used as the type of a dependency property. I feel that i've heard about problems, but can't remember what it was exactly. – Clemens Apr 03 '12 at 18:20
  • No, this is still wrong, the reason the property is null is because the property cannot have a value on an instance of a class that is not of the registered type. I've encountered this problem before in a production system. – Alain Apr 03 '12 at 18:26
  • @Alain not for **attached properties**. Please see [here](http://stackoverflow.com/a/9915060/1136211) and really, **try it**. According to your explanation, it would be impossible to set `Canvas.Left` on a class that is not a `Canvas`. Obviously nonsense. – Clemens Apr 03 '12 at 18:27
  • @Clemens, all right, it was another error, I could get the value. – basilkot Apr 03 '12 at 18:28