是否可以在 Sealed XAML 控件类上创建附加属性? [英] Is it possible to create an Attached Property on a Sealed XAML control class?
问题描述
与旧的桌面版本不同,Windows 应用商店应用程序库中的 Run 类是密封的.我需要向它添加一个属性,当用户选择运行时我可以检索该属性.
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.
是否可以将属性附加到我可以在代码中访问的密封 Run 类?
Is it possible to attach a property to the sealed Run class that I can access in code?
这是我的尝试:
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);
}
推荐答案
C# 语言 不支持扩展"或附加"属性.
The C# language does not support "extension" or "attached" properties.
附加属性是影响 XAML 和 WPF 绑定系统的 WPF 功能.在代码中使用 WPF 附加属性的唯一方法是编写 YourClass.SetMyIndex(instance, value);
.
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.
这篇关于是否可以在 Sealed XAML 控件类上创建附加属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!