Clear dependencyProperty value from xaml

I have a control that has a default value for a property. When a control first gets its data set, it automatically assigns this property.

In xaml now, I want this property to be UNset. I tried setting it to x: Zero is just an empty string, but then I get an error because there is no converter for the property. How to simply disable this property from xaml in rare cases when I want a function to be disabled?

where it was originally installed:

void OmniBox_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e) { if( e.NewValue is BindingObjectBaseExtended ) { BindingObjectBaseExtended value = (BindingObjectBaseExtended)e.NewValue; this.SetBinding(OmniBox.ContextValidationMessagesProperty, new Binding() { Source = value, Path = new PropertyPath("ValidationMessages") }); } } 

xaml where I want to disable the property.

 <Style TargetType="ui:OmniBox"> <Setter Property="ContextValidationMessages" Value="" /> </Style> 

Please note that if I do not configure the binding automatically when changing the data context, there are no validation messages by default, and I have to do the following in xaml to configure them:

 <Style TargetType="ui:OmniBox"> <Setter Property="ContextValidationMessages" Value="ValidationMessages" /> </Style> 

What I'm trying to do is make a default binding for my custom OmniBox control and let the user undo or change it to something else.

+4
source share
4 answers

Personally, I create a separate dependency property, such as bool AutoBindValidation , and make it the default value true. If it is incorrect, do nothing when the DataContext changes. This is a little more self-documenting. Depending on what you are trying to do, you may not publicly publish ContextValidationMessages at all.

If you really want to do it the way you posted, I'm not sure why setting it to {x:Null} will result in an error (if the property type is not null). But this approach will have problems because the DataContextChanged will occur after the XAML analysis. Thus, the user can set it to {x:Null} , but then the DataContext will change, and your code will set the default binding and stomp the value of the user. You can configure the binding in the control's constructor, but then if the DataContext does not have the ValidationMessages property, your control will spit out binding errors.

+2
source

This may not be possible, my best bet is:

 <Setter Property="ContextValidationMessages" Value="{x:Static DependencyProperty.UnsetValue}" /> 

But that throws a "Cannot unset setter value" . This way you better invert your logic or save the property in another way.

+1
source

I don't think there is any supported way to do this in haml itself. In your code, you set the local value to ContextValidationMessagesProperty . The style organizers that you included would have a lower priority for the dependency property , and even if they were evaluated, they would set the value based on the specified value - do not clear it. Perhaps instead of setting a binding in the code, you might have a default style setter for the OmniBox that sets this property - for example,

 <Setter Property="ContextValidationMessages" Value="{Binding ValidationMessages}" /> 

If you need to conditionally set the binding, you can create a custom IValueConverter that checks the specified type (passed as a parameter). eg.

 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 can use it as follows:

  <local:IsAssignableFromConverter x:Key="isAssignableConverter" /> <Style TargetType="ui:OmniBox"> <Style.Triggers> <DataTrigger Binding="{Binding Converter={StaticResource isAssignableConverter}, ConverterParameter={x:Type ui:BindingObjectBaseExtended}}" Value="True"> <Setter Property="ContextValidationMessages" Value="{Binding ValidationMessages}" /> </DataTrigger> </Style.Triggers> </Style> 

In the case when you do not want this property to be applied, you can set the style for this OmniBox instance to a new style and make sure to set the OverridesDefaultStyle property to true .

I believe another option is to create another dependency property that will call ClearValue in the ContextValidationMessages property, but it looks like this might be a maintenance issue.

+1
source

Source: https://habr.com/ru/post/1379167/


All Articles