Is it possible to create an Attached Property on a Sealed XAML control class?

◇◆丶佛笑我妖孽 提交于 2019-12-13 04:29:00

问题


The Run class in the Windows Store Apps libraries is SEALED, unlike the old desktop version. I need to add a property to it that I can retrieve when the user selects the Run.

Is it possible to attach a property to the sealed Run class that I can access in code?

Here is my attempt:

public static readonly DependencyProperty MyIndexProperty =
    DependencyProperty.RegisterAttached("MyIndex", typeof(int), typeof(Run), new PropertyMetadata(null));

public static int GetMyIndex(Run obj)
{
    return (int)obj.GetValue(MyIndexProperty);
}

public static void SetMyIndex(Run obj, int value)
{
    obj.SetValue(MyIndexProperty, value);
}

回答1:


The C# language does not support "extension" or "attached" properties.

Attached properties are a WPF feature which affect XAML and the WPF binding system. The only way to use a WPF attached property in code is to write YourClass.SetMyIndex(instance, value);.

There is no way to accomplish what you want, although extension methods can come close.



来源:https://stackoverflow.com/questions/16701467/is-it-possible-to-create-an-attached-property-on-a-sealed-xaml-control-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!