Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

wpf

I have created a custom MultiValue Converter to perform a bit of logic while MultiBinding to a TextBox; however I do not want to use the convertBack since the binding value does not have a compile-time type and the default conversion works perfectly. Is this possible or do I have to somehow replicate the functionailty of one of the internal default converters?

Diagram:

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

Thank you.

Edit: Forgot to mention that I am using a MultiValueConverter and MultiBinding which seems to bypass the default converter.

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

Edit: I have solved the issue in a roundabout fashion - please see my response below. If you have a better solution I would still appreciate hearing it. Thanks again for your time and help.

like image 274
Andrew Hanlon Avatar asked Dec 04 '22 22:12

Andrew Hanlon


2 Answers

Just return value in convertback

    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;
    }
like image 186
Arsen Mkrtchyan Avatar answered Dec 24 '22 11:12

Arsen Mkrtchyan


Okay I just figured out a solution... Thanks to everyone for posting suggestions.

It turns out that if you create a pass-through ValueConverter (as in just return value for both Convert and ConvertBack), and add it to the sub-Bindings of the MultiBinding, that the default conversions will happen as expected. What must be happening is that the MultiBinding in general bypasses the defaultConverter of the sub-Bindings.

like image 23
Andrew Hanlon Avatar answered Dec 24 '22 10:12

Andrew Hanlon