views:

63

answers:

2

Hi all, I'm a newbie to Silverlight and I'm having some trouble finding a solution to an issue.

I have a silverlight datagrid with 3 columns. One of the columns is bound to an integer. I want to be able to bind my column to a function that would translate my integer into it's status code. The function accepts an integer, and using a switch statement I return a string of what that number represents.

0 = Inactive
1 = Active
2 = Pending
etc

A lot of what I've found has been techniques for Element Binding, which is very cool, but not what I'm looking for.

+1  A: 

Hi ,
You can create an IValueConverter that provides you the ability to invoke a function on the databound value.
You can customize the Convert method to return a string based on the value passed in :

public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
switch((int)value)
{
case 0: return "Inactive";
case 1: return "Active";
case 2: return "Pending";
}
}

IValueConverter on MSDN
IValueConverter example in Silverlight

Phani Raj
A: 

Depending on your architecture I would either

  1. Implement an IValueConverter like Phani suggests
  2. Use the Model-View-ViewModel(MVVM) pattern. In this pattern, anything needed for the binding would be represented as addition property in your view model.

so you would have something like the following

public class ViewModel:INoftifyPropertyChanged
{
 private Model _model;

 public string StatusCodeName
 {
  get
  {
   string statusCodeName = SomeCodeToGetStatusCodeNameFromStatus(_model.Status);
   return statusCodeName;
  } 
 }
}

You can then bind to this property

{Binding StatusCodeName}
Jacob Adams