问题
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