c# - WPF - 数据绑定(bind)时是否正在进行某种隐式转换?

标签 c# wpf data-binding converters

我刚刚注意到,在我的项目的某些地方,我忘记了将我的 Int/DoubleToStringValueConverter 与我的 TwoWay 绑定(bind)一起使用,但它们仍然可以正常工作。

那么那里有某种隐式转换吗?我实际上知道 string->color、color->string 转换,我有时会使用它,但不会使用 number->string、string->number。那么,您能否在某个地方推荐一份文档,让我可以阅读有关何时应该使用转换器以及何时不应该使用转换器的信息?

最佳答案

来自Data Conversion Data Binding Overview 部分关于 MS Docs 的文章:

[...] default conversions may be available because of type converters that are present in the type being bound to. This behavior will depend on which type converters are available in the target. If in doubt, create your own converter.

The following are some typical scenarios where it makes sense to implement a data converter:

• Your data should be displayed differently, depending on culture. For instance, you might want to implement a currency converter or a calendar date/time converter based on the conventions used in a particular culture.

• The data being used is not necessarily intended to change the text value of a property, but is instead intended to change some other value, such as the source for an image, or the color or style of the display text. Converters can be used in this instance by converting the binding of a property that might not seem to be appropriate, such as binding a text field to the Background property of a table cell.

• More than one control or multiple properties of controls are bound to the same data. In this case, the primary binding might just display the text, whereas other bindings handle specific display issues but still use the same binding as source information.

• A target property has a collection of bindings, which is termed MultiBinding. For MultiBinding, you use a custom IMultiValueConverter to produce a final value from the values of the bindings. For example, color may be computed from red, blue, and green values, which can be values from the same or different binding source objects. See MultiBinding for examples and information.

关于c# - WPF - 数据绑定(bind)时是否正在进行某种隐式转换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24781638/

相关文章:

c# - 什么是 IF 语句中的 OR 运算符

wpf - ListView.ItemTemplate中的SharedSizeGroup

c# - 将 DataGridView 绑定(bind)到对象列表

c# - 在可见性更改时激活 Storyboard

c# - 如何在没有我的 WPF 主机的情况下启动具有管理员权限的进程也以管理员权限运行以实现文件拖放?

wpf - 使用 Caliburn micro 的动态菜单

c# - 如何将 Combobox 绑定(bind)到 ReactiveUI 中的命令?

java - 重构:用方法对象替换方法解释

c# - 在 HoloLens 1 上,在使用默认构造函数 "ArgumentException: Value does not fall within the expected range"创建 TcpClient 对象时抛出

c# - DateTimeFormatInfo.DayNames 在 Android 上返回额外的空日期名称