Why do we have both TypeConverters and IValueConverter in WPF?

拈花ヽ惹草 提交于 2021-02-07 13:47:58

问题


I am new to WPF. I just don't understand why there is a need for TypeConverters and IValueConverter in WPF. The purpose of both objects is to convert a value to specific type; but why both?

Thanks in advance.


回答1:


IValueConverter is used only in data-binding scenarios. It allows you to format values before they are displayed in the UI or to parse values from UI controls so that they can be stored in the binding source. an example would be to convert an IsDirty flag to an "*" in the UI or a Color instance to a Brush instance. from my own experience i've used IValueConverter just for formatting purposes (that is, only implementing the Convert method).

A TypeConverter derived class is mostly used to convert to/from string values. this can be used for setting a property of a custom type in XAML.



来源:https://stackoverflow.com/questions/24607667/why-do-we-have-both-typeconverters-and-ivalueconverter-in-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!