开发者

Using the Same FrameworkPropertyMetaData more than once

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜