Is there a way to make two-way MultiBinding use the converter in only one direction?

I created a custom MultiValue converter to execute a bit of logic, and MultiBinding for a TextBox; however, I do not want to use convertBack, since the binding value is not of compile time type, and the default conversion works fine. Is this possible, or do I need to somehow replicate the function of one of the internal default converters?

Diagram:

values --> Convert() --> TextBox values <---------------- TextBox 

Thanks.

Edit: forgot to mention that I use MultiValueConverter and MultiBinding, which seem to bypass the default converter.

Edit: To expand the reasoning behind this: I have two objects A and B (of the same type) that I want to edit at the same time in a TextBox. In the Convert method, I check if they are the same value and either display the value or the default value. If the user changes the value in the TextBox, I want the same value to be sent back to A and B.

Edit: I solved the problem in circular mode - see my answer below. If you have a better solution, I will still be grateful for that. Thanks again for your time and help.

+4
source share
5 answers

Ok, I just figured out the solution ... Thanks to everyone who posted the suggestions.

It turns out that if you create an end-to-end ValueConverter (as in just the return value for Convert and ConvertBack) and add it to the sub-Bindings of MultiBinding, the default conversions will be executed as expected. What should happen is that MultiBinding generally bypasses the standard sub-binding converter.

+5
source

Just return the value in convertible

  public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { return YourLogic(value); } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { return value; } 
+6
source

No, it is impossible to bypass the converter.

As @ArsenMkrt explained, you will have to pass the original values ​​through the converter.

For MultiBinding, this is difficult because you should be able to turn 1 value into N. You may have to store the information in the converter in the first pass to help you do this. I rarely used double-sided MultiBindings mainly because it is difficult to convert back from a single value to many values.

It is still missing here. Why do you want to pass the default value through MultiBinding? It's impossible. MultiBinding always needs a converter, because you add several values ​​to one, and then expand one value to many.

+4
source

How about this:

 MyType Default { get; set; } public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) { // compare values, return value if equal or default } public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) { return Enumerable.Repeat(value, targetTypes.Length).ToArray(); } 

The returned function returns the value back to all MultiBinding sources.

+1
source

You cannot use the default conversion and use the custom value converter at the same time. Thus, you will need to implement the default conversion in your custom value converter. Something like this uses the standard TypeConverter of the target type:

 public class MyConverter : IMultiValueConverter { public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) { return System.Convert.ToString(values[0]); } public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) { // Get the type of the value, but assume string if it null Type valueType = typeof(string); if (value != null) valueType = value.GetType(); // Convert value to the various target types using the default TypeConverter object[] values = new object[targetTypes.Length]; for (int i = 0; i < values.Length; i++) { TypeConverter typeConverter = TypeDescriptor.GetConverter(targetTypes[i]); if (typeConverter != null && typeConverter.CanConvertFrom(valueType)) values[i] = typeConverter.ConvertFrom(value); else values[i] = value; } return values; } } 
0
source

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


All Articles