Personally, I would create a separate dependency property, such as bool AutoBindValidation and make it default to true. If it is false, don't do anything when the DataContext changes. This is a little more self-documenting.
Depending on what exactly you're trying to do, you might not want to publicly expose ContextValidationMessages at all If you really want to do it the way you posted, I'm not sure why setting it to {x:Null} would cause an error (unless the property type is not nullable). But this approach would have problems because DataContextChanged is going to occur after the XAML is parsed. So the user can set it to {x:Null} but then the DataContext will change and your code will set up the default binding and trample the user's value.
You could set up the binding in the control's contstructor, but then if the DataContext does not have a ValidationMessages property, your control will be spitting out binding errors.
Personally, I would create a separate dependency property, such as bool AutoBindValidation and make it default to true. If it is false, don't do anything when the DataContext changes. This is a little more self-documenting.
Depending on what exactly you're trying to do, you might not want to publicly expose ContextValidationMessages at all. If you really want to do it the way you posted, I'm not sure why setting it to {x:Null} would cause an error (unless the property type is not nullable). But this approach would have problems because DataContextChanged is going to occur after the XAML is parsed.So the user can set it to {x:Null}, but then the DataContext will change and your code will set up the default binding and trample the user's value.
You could set up the binding in the control's contstructor, but then if the DataContext does not have a ValidationMessages property, your control will be spitting out binding errors.
1 The correct solution in the case may be to create a new AutoBindValidation dependency property defaulting to true, and just provide an error message if they try to set ContextValidationMessages without first setting this to false. For my application though, I do want it to be possible to override the default auto-binding with different ValidationMesages collections. – Alain Nov 1 at 17:45.
This may be impossible, my best bet was this: But that throws "Cannot unset setter value". So you better inverse your logic or keep the property unset another way.
1 I wasn't aware of {x:Static DependencyProperty. UnsetValue}, even if it doesn't help this case, it might help similar ones. – Alain Nov 1 at 17:40.
I don't think there is any supported way to do this in the xaml itself. In your code you are setting a local value on the ContextValidationMessagesProperty. The Style setters you included would have a lower dependency property precedence and even if they were evaluated they would set a value based on the specified Value - not clear it.
Maybe instead of setting the binding in code you could have a Setter in your default style for OmniBox that sets that property - e.g. If you have to conditionally set the Binding then you could create a custom IValueConverter that checks for the specified type (passed as the parameter). E.g. Public class IsAssignableFromConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { Type typeParameter = parameter as Type; if (typeParameter == null) return DependencyProperty.
UnsetValue; return value! = null && typeParameter. IsAssignableFrom(value.GetType()); } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { return DependencyProperty.
UnsetValue; } } Then you might use it like this: In the case where you don't want this property to be applied you might set the Style for that instance of the OmniBox to a new style and make sure to set the OverridesDefaultStyle property to true. I suppose another option is to create another dependency property that will call ClearValue on the ContextValidationMessages property but this seems like it could be a maintenance issue.
Problem is that I really need to check the type of the dataContext before I can attempt to set the ContextValidationMessages. I only want it automatically set for bound objects which inherit from the BindingObjectBaseExtended class, otherwise the onus is definitely on the user to set it. I can't do that type checking in the xaml.
– Alain Nov 1 at 17:47 I updated the answer to use a custom IValueConverter that you could use in a Style Trigger to conditionally apply the setter if the DataContext is a particular type (or derived). – AndrewS Nov 1 at 18:26.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.