开发者

Attached Properties in XAML without the word 'Property'

开发者 https://www.devze.com 2023-01-20 05:35 出处:网络
MS defines attached properties like \'Grid.RowProperty\' and \'Grid.ColumnProperty\', but in XAML you just call it with \'Grid.Row\' and \'Grid.Column\'

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 a开发者_Go百科ttached 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消