Im trying to bind an image to an Id in a grid where I show all my users. Does anybody knows how to do this? I've got a table User where there is a field called StatusId.
There are 2 kinds of status
Status1: Id:10 | active Status2: Id:90 | Non-active
When the status is 10 I want to show a green cross, when it's 90 I want to show Red cross.
I'm working with the Mvvm pattern.
Here is my gridcode.
<telerik:RadGridView Name="RadGridView1" ItemsSource="{Binding CovrUsers,Mode=TwoWay}" IsReadOnly="True" DataContext="{Binding Source={StaticResource CovrUsersViewModel}}" AutoGenerateColumns="False" Margin="230 0 0 0 " Height="Auto">
<telerik:RadGridView.Columns>
<telerik:GridViewDataColu开发者_开发百科mn Header="First name" DataMemberBinding="{Binding FirstName}" />
<telerik:GridViewDataColumn Header="Last name" DataMemberBinding="{Binding LastName}" />
<telerik:GridViewDataColumn Header="User name" DataMemberBinding="{Binding UserName}" />
<telerik:GridViewDataColumn Header="Hourly rate" DataMemberBinding="{Binding HourlyRate}" />
<telerik:GridViewDataColumn Header="Rights" DataMemberBinding="{Binding Rights.Description}" />
<telerik:GridViewDataColumn Header="Status" DataMemberBinding="{Binding StatusId}" />
</telerik:RadGridView.Columns>
</telerik:RadGridView>
<telerik:RadDataPager x:Name="RadDataPager1" Source="{Binding CovrUsers, ElementName=FirstName}" DisplayMode="All" IsTotalItemCountFixed="True" Margin="230,0,0,0" VerticalAlignment="Bottom" FontSize="11" />
thx for the help :)
You'll want to use a ValueConverter to convert your Status value into an image. Something like this:
public class CommentTypeIconConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if(value != null)
{
int ct = int.Parse(value.ToString());
if(ct == 10)
{
StreamResourceInfo sr = Application.GetResourceStream(
new Uri("Assets/greencross.png", UriKind.Relative));
BitmapImage bmp = new BitmapImage();
bmp.SetSource(sr.Stream);
return bmp;
}
else
{
StreamResourceInfo sr = Application.GetResourceStream(
new Uri("Assets/redcross.png", UriKind.Relative));
BitmapImage bmp = new BitmapImage();
bmp.SetSource(sr.Stream);
return bmp;
}
}
else
{
StreamResourceInfo sr = Application.GetResourceStream(
new Uri("Assets/empty.png", UriKind.Relative));
BitmapImage bmp = new BitmapImage();
bmp.SetSource(sr.Stream);
return bmp;
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
Read this link to learn about how to use the value converter on your page.
精彩评论