简体   繁体   中英

Correct way to not implement ConvertBack on a IMultiValueConverter

I have an object that implements IMultiValueConverter . It is used to bind visibility of a column based upon a particular permutation of a specific bool value and a specific enum value, both part of the bound data. The ConvertBack method self evidently has no meaning.

On a regular IValueConverter, I could return Binding.DoNothing , but that is not an object[] so won't compile.

I currently throw an exception, but that doesn't feel ideal. Is there a better way?

The correct way to implement the ConvertBack method of an IValueConverter or an IMultiValueConverter that does not support back-conversion is to throw a NotSupportedException .

Returning Binding.DoNothing makes no sense, as the method should never be called anyway. But if it is ever called unexpectedly, you would rightly get an exception that tells you what went wrong.

public object[] ConvertBack(
    object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
    throw new NotSupportedException();
}

Return null

For IMultiValueConverter , the ConvertBack documentation specifically states:

Return null to indicate that the converter cannot perform the conversion or that it does not support conversion in this direction .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM