0

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

Eamonn McEvoy
  • 8,876
  • 14
  • 53
  • 83

2 Answers2

1

You need to declare a new one for each.

Gray
  • 7,050
  • 2
  • 29
  • 52
Elad Katz
  • 7,483
  • 5
  • 35
  • 66
1

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.

bsazonov
  • 71
  • 2