简体   繁体   中英

Using the Same FrameworkPropertyMetaData more than once

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

你需要为每个声明一个新的。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM