多次使用相同的 FrameworkPropertyMetaData

发布于 2024-10-28 07:48:41 字数 803 浏览 4 评论 0原文

我有 3 个依赖属性和一个 FrameworkPropertyMetadata,当我尝试在多个 DP 上使用元数据时,我遇到了崩溃。我不想有 3 个重复的元数据,有办法解决这个问题吗?

    static FrameworkPropertyMetadata propertyMetaData = new FrameworkPropertyMetadata("My Control", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault);

    public static readonly DependencyProperty Property_A = DependencyProperty.Register("Property_A", typeof(string), typeof(MyControl), propertyMetaData);
    public static readonly DependencyProperty Property_B = DependencyProperty.Register("Property_B", typeof(string), typeof(MyControl), propertyMetaData);
    public static readonly DependencyProperty Property_C = DependencyProperty.Register("Property_C", typeof(string), typeof(MyControl), propertyMetaData);

我是否需要为每个属性声明单独的元数据,还是可以使用相同的元数据?

谢谢, 埃蒙

I have 3 dependancy Properties and a FrameworkPropertyMetadata, I get a crash when I try to use the metadata on more than one DP. I dont want to have 3 duplicates of the metadatam is there a way around this.

    static FrameworkPropertyMetadata propertyMetaData = new FrameworkPropertyMetadata("My Control", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault);

    public static readonly DependencyProperty Property_A = DependencyProperty.Register("Property_A", typeof(string), typeof(MyControl), propertyMetaData);
    public static readonly DependencyProperty Property_B = DependencyProperty.Register("Property_B", typeof(string), typeof(MyControl), propertyMetaData);
    public static readonly DependencyProperty Property_C = DependencyProperty.Register("Property_C", typeof(string), typeof(MyControl), propertyMetaData);

Do I need to declare a seperate metadata for each property or can I use the same one?

Thanks,
Eamonn

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我一向站在原地 2024-11-04 07:48:41

如果您想避免代码重复(这看起来很合理),您可以编写类似于以下的简单实用方法:

private internal static FrameworkPropertyMetadata CreateDefaultPropertyMetadata()
{
   return new FrameworkPropertyMetadata("My Control", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault);
}

然后使用它:

public static readonly DependencyProperty Property_A = DependencyProperty.Register("Property_A", typeof(string), typeof(MyControl), CreateDefaultPropertyMetadata());
public static readonly DependencyProperty Property_B = DependencyProperty.Register("Property_B", typeof(string), typeof(MyControl), CreateDefaultPropertyMetadata());
public static readonly DependencyProperty Property_C = DependencyProperty.Register("Property_C", typeof(string), typeof(MyControl), CreateDefaultPropertyMetadata());

如果我正在解释显而易见的事情,请原谅。

If you want to avoid code repeating (which seem reasonable), you can write simple utility method similar to:

private internal static FrameworkPropertyMetadata CreateDefaultPropertyMetadata()
{
   return new FrameworkPropertyMetadata("My Control", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault);
}

And then use it:

public static readonly DependencyProperty Property_A = DependencyProperty.Register("Property_A", typeof(string), typeof(MyControl), CreateDefaultPropertyMetadata());
public static readonly DependencyProperty Property_B = DependencyProperty.Register("Property_B", typeof(string), typeof(MyControl), CreateDefaultPropertyMetadata());
public static readonly DependencyProperty Property_C = DependencyProperty.Register("Property_C", typeof(string), typeof(MyControl), CreateDefaultPropertyMetadata());

Excuse me if I'm explaining obvious things.

橘寄 2024-11-04 07:48:41

您需要为每一项声明一个新的。

You need to declare a new one for each.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文