开发者

Using the Same FrameworkPropertyMetaData more than once

开发者 https://www.devze.com 2023-02-21 17:34 出处:网络
I have 3 dependancy Properties and a开发者_Go百科 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 ther

I have 3 dependancy Properties and a开发者_Go百科 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


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.


You need to declare a new one for each.

0

精彩评论

暂无评论...
验证码 换一张
取 消