Question

What are the recommended uses of IValueConverter? I've used it for things like converting a boolean property to a Visibility property but I'm wondering how far you should take it? Is it supposed to be only used to change representations of common types to UI specific types (bool to Visibility for example).

E.g., if I have a property in my class, EmployeeID, like:

public class EmployeeViewModel : INotifyPropertyChanged
{
  public int? EmployeeID
  {
    get
    {
      return employeeID;
    }
    set
    {
      employeeID = value;
      RaisePropertyChanged("EmployeeID");
    }
  }
}

and I want to display details for this employee, e.g. Name, location but to get these I would need to hit the Database, would this be wrong to put this into a Value Converter? Would it be better practice to store the values in the ViewModel and bind to them?

Was it helpful?

Solution

It would probably be better practice to put the values in your ViewModel and do direct bindings. While you could do it in the ValueConverter, you have a very limited scope in the converter and that can limit your capabilities.

My general rule for myself is to try to keep all my ValueConverters as simple conversions between two types, i.e. Bool to Visibility or a Color to a Brush. That way, I'm not including much logic in the ValueConverters and keeps everything simpler to maintain.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top