本文介绍了为什么IMultiValueConverter.ConvertBack甚至存在?有人实施过吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据,实现IMultiValueConverter.ConvertBack的正确方法是抛出NotSupportedException。如果是这样,为什么还要使用这种方法呢?

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?

推荐答案

是。


Yes.


哦,很抱歉。您还想要什么吗? :)

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

您误解了所引用的问题。问题本身清楚地表明,他们的特定方案对于 ConvertBack()方法没有有意义的用途,并且他们正在询问该方法在特定情况下应该做什么。发布的答案针对的是特定情况,而不是 IMultiValueConverter 的每个单独实现。

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. 合并三个颜色分量的转换器红色,绿色和蓝色–放入一个 Color 值以绑定到某种类型的颜色选择器控件。 Convert()方法会将三个通道组成单个 Color 值,而 ConvertBack()方法会将 Color 的值分解为三个组成属性。

  2. 格式化的转换器表示为三个不同属性的时间值–小时,分钟和秒钟–作为格式为 HH:MM:SS 的文本字符串。 Convert()方法将值与':'分隔符组合在一起,而 ConvertBack()方法会将这些值分开并将其解析为原始整数值。

  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.

这些只是几个合理的例子。当然,在现实世界中,有很多原因可能实际上为 ConvertBack()方法提供了一个真正的实现。

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.

当然,在很多情况下,绑定都是单向的,并且没有实现 ConvertBack()方法的有意义的方法。对于这些情况,抛出 NotSupportedException(),就像。

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.

这篇关于为什么IMultiValueConverter.ConvertBack甚至存在?有人实施过吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 12:04