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