【问题标题】:Is it possible to create an Attached Property on a Sealed XAML control class?是否可以在 Sealed XAML 控件类上创建附加属性?
【发布时间】:2013-05-22 21:00:00
【问题描述】:

Windows 应用商店应用程序库中的 Run 类是 SEALED 的,与旧桌面版本不同。我需要向它添加一个属性,当用户选择 Run 时我可以检索它。

是否可以将属性附加到我可以在代码中访问的密封 Run 类?

这是我的尝试:

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# 代码中访问 Run.MyIndex 将无法编译。代码在同一个命名空间中。

标签: c# xaml windows-store-apps


【解决方案1】:

C#语言不支持“扩展”或“附加”属性。

附加属性是影响 XAML 和 WPF 绑定系统的 WPF 功能。 在代码中使用 WPF 附加属性的唯一方法是编写 YourClass.SetMyIndex(instance, value);

没有办法实现你想要的,尽管扩展方法可以接近。

【讨论】:

  • 谢谢。希望他们在跑步时添加了标签或其他东西。我将不得不想出另一种解决方案来识别它们,比如使用名称字段或其他东西。我们的应用正在显示用户选择它们时需要识别的编号文本,以便我们可以显示相关媒体等。
猜你喜欢
  • 1970-01-01
  • 2014-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多