views:

23

answers:

2

MS defines attached properties like 'Grid.RowProperty' and 'Grid.ColumnProperty', but in XAML you just call it with 'Grid.Row' and 'Grid.Column'

I tried doing the same with an attached property called 'MyValProperty' that was registered with the name 'MyVal' on the class 'Foo', but the XAML won't let me type 'Foo.MyVal' and instead makes me type 'Foo.MyValProperty' How is MS doing that? What am I missing?

A: 

Are you correctly passing the name of your property to RegisterAttached?

vc 74
Yeah, it was the static getter/setter I had messed up.
MarqueIV
+2  A: 

As background here's how register an attached property

public static readonly DependencyProperty IsBubbleSourceProperty = DependencyProperty.RegisterAttached(
    "IsBubbleSource",
    typeof(Boolean),
    typeof(AquariumObject),
    new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender)
);
public static void SetIsBubbleSource(UIElement element, Boolean value)
{
    element.SetValue(IsBubbleSourceProperty, value);
}
public static Boolean GetIsBubbleSource(UIElement element)
{
    return (Boolean)element.GetValue(IsBubbleSourceProperty);
}

You can also use a standard property format rather than the Set/Get construct. This is one of those areas where WPF has a strong convention in place. There are three parts of an AttachedProperty (or any DependencyProperty). The first is registering the property with DependencyProperty.RegisterAttached and the return value should be set to a public static variable named [Property Name]Property. The second is that the first argument when registering your property should be "[Property Name]". And the third is the Get/Set methods you'll use to interact with the outside world which should be named Get[Property Name], Set[Property Name].

If you set it up according to the convention WPF will recognize that the two are connected and should allow you to use the property as you expect.

Bryan Anderson
Ha! I thought I had registered it correctly. I did, but I accidentally used the wrong name in the GetXxx/SetXxx static calls (I had GetXxxProperty/SetXxxProperty). Oversight on my part. Marked as the answer.
MarqueIV
BTW, you said 'You can also use a standard property format rather than the Set/Get construct.' Didn't think you could do that with attached properties. Got an example?
MarqueIV
hmm, I think you can but I haven't found any examples in code I have available to me. Granted I don't use attached properties too often so I'd just stick with what works.
Bryan Anderson
Yeah... not sure you can since the getter and setter are static. Granted, you can probably make them extension methods, but there isn't extension properties in C#. Plus, there are the parameters which again I'm pretty sure precludes using a property. Still, you were spot-on in showing that my getter/setter had the wrong names which stopped the compiling of the XAML (which is odd when you think about it since XAML at runtile just bypasses that anyway and uses the name you registered it with! Go figure!)
MarqueIV
Okay, I edited the answer to remove the reference to standard properties.
Bryan Anderson