Silverlight Datagrid Databinding
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 = Activ开发者_运维知识库e 2 = Pending etcA lot of what I've found has been techniques for Element Binding, which is very cool, but not what I'm looking for.
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
Depending on your architecture I would either
- Implement an IValueConverter like Phani suggests
- 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}
精彩评论