How to set metadata options without setting a default value?
When registering a dependency property how do I set metadata o开发者_JAVA技巧ptions without setting a default value?
You could do this with Object Initializer
public static readonly DependencyProperty MyDependencyProperty =
DependencyProperty.Register("MyDependency",
typeof(propertyType),
typeof(ownerType),
new FrameworkPropertyMetadata {
BindsTwoWayByDefault = true,
PropertyChangedCallback = OnPropertyChanged,
... etc ...
});
There are four constructors available for PropertyMetadata which you can find here. You can use the third one which doesn't take any default value parameter.
PropertyMetadata(PropertyChangedCallback)
public static readonly DependencyProperty SomeProperty = DependencyProperty.Register("SomeName", typeof(string), typeof(SomeClass),
new PropertyMetadata(SomeChangedCallback),
SomeValidateCallback);
精彩评论