Hi,
you can use IValueConverter as below. I have pasted here a example have a look.
IValueConverter convertor Reference in XAML
xmlns:convertor="clr-namespace:ApplicationShell"
Add Window Resource
<Window.Resources>
<convertor:InttoString x:Key="inttoString"/>
</Window.Resources>
My XAML
<datagrid x:name="studentDG" height="150" autogeneratecolumns="False" xmlns:x="#unknown">
<datagrid.columns>
<datagridtextcolumn binding="{Binding IDs, Converter={StaticResource inttoString}}" header="ID" />
</datagrid.columns>
</datagrid>
My Code behind
List<teststu> IDs = new List<teststu>();
IDs.Add(new TestStu { IDs = 1 });
IDs.Add(new TestStu { IDs = 2 });
IDs.Add(new TestStu { IDs = 3 });
IDs.Add(new TestStu { IDs = 4 });
studentDG.ItemsSource = IDs;
and my IValueConverter
public class InttoString : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
switch (value + "")
{
case "1":
return "One";
case "2":
return "Two";
case "3":
return "Three";
case "4":
return "Four";
default:
return "Zero";
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
let me know if you can't understand.