Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

According to this accepted answer, the proper way to implement IMultiValueConverter.ConvertBack is to throw a NotSupportedException. If that is the case, why does this method even exist?

Maybe it's not true. Certainly Microsoft's documentation discusses all sorts of a ways for an implementation to indicate it does not support the conversion that do not involve throwing an exception. But I have never seen an implementation that actually did the conversion.

Has anyone ever actually implemented this function?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
141 views
Welcome To Ask or Share your Answers For Others

1 Answer

Has anyone ever actually implemented this function?

Yes.


Oh, I'm sorry. Did you want something more than that? :)

You have misunderstood the question you're referencing. The question itself makes clear that their specific scenario has no meaningful use for the ConvertBack() method, and they are asking what the method should do in that specific case. The posted answer addresses that specific case, not every single implementation of IMultiValueConverter.

I have certainly implemented the method myself with actual logic in the past. A couple of examples where you might find doing so useful are:

  1. A converter that merges three color components – red, green, and blue – into a single Color value for binding to some type of color-picker control. The Convert() method would compose the three channels into the single Color value, while the ConvertBack() method would decompose the Color value back to three constituent properties.
  2. A converter that formats a time value represented as three different properties – hours, minutes, and seconds – as a text string with the format "HH:MM:SS". The Convert() method would combine the values with the ':' separator character, while the ConvertBack() method would split the values apart and parse them back as their original integer values.

Those are just a couple of reasonable examples. Of course in the real-world, there are countless reasons one might actually provide a real implementation for the ConvertBack() method.

Of course, there are just as many scenarios where the binding is one-way only, and there's no meaningful way to implement the ConvertBack() method. For those scenarios, throw NotSupportedException(), just as the referenced Q&A suggests.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...