How to dynamically link image for datagrid in.cs
this is my xaml code.
<sdk:DataGrid x:Name="dgMarks" CanUserResizeColumns="False" SelectionMode="Single" AutoGenerateColumns="False" VerticalAlignment="Top" IsReadOnly="True" Margin="13,44,0,0" RowDetailsVisibilityChanged="dgMarks_RowDetailsVisibilityChanged" RowDetailsVisibilityMode="Collapsed" Height="391" HorizontalAlignment="Left" Width="965" SelectionChanged="dgMarks_SelectionChanged" VerticalScrollBarVisibility="Visible" >
<sdk:DataGrid.Columns>
<sdk:DataGridTemplateColumn>
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button x:Name="myButton"
Click="ExpandMarks_Click">
<TextBlock Text="{Binding Level}" TextWrapping="NoWrap" ></TextBlock>
<Image x:Name="imgMarks" Stretch="None"/>
</Button>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
<sdk:DataGridTemplateColumn Header="Name" Visibility="Collapsed">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate >
<sdk:Label Content="{Binding Name}"/>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
<sdk:DataGridTemplateColumn Header="Marks" Width="80">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<sdk:Label Content="{Binding Marks}"/>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
</sdk:DataGrid.Columns>
</sdk:DataGrid>
from the database i get these values
name marks Level
abc 23 0
xyz 67 1
yu 56 0
aa 89 1
here i am linking these values for datagrid. I have a tricky thing to do. Based on the level, I must be a required image if the level value is 1, then anchor the image. if the level value is 0 then don't bind the image for this line
I know what we have to handle, but where should I write this code in which events?
Image imgLevel = (Image)templateTrendScore.FindName("imgMarks");
if (level1==1)
{
imgLevel .Source = new BitmapImage(new Uri("/Images/image1.JPG", UriKind.Relative));
}
any help would be greatly appreciated in advance
a source to share
A value converter is an item that you use if you have more than 0 and 1 for the level (otherwise would you use boolean right?)
Here is the value converter: -
[ContentProperty("Items")]
public class IndexToObjectConverter : IValueConverter
{
private readonly ObservableCollection<object> myCol = new ObservableCollection<object>();
public ObservableCollection<object> Items { get { return myCol; } }
#region IValueConverter Members
public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return Items[(int)value];
}
public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException("This converter only works for one way binding");
}
#endregion
}
In usercontrol resources you will have the following: -
<UserControl.Resources>
<local:IndexToObjectConverter x:Key="LevelToImage">
<BitmapImage />
<BitmapImage UriSource="Test.png" />
</local:IndexToObjectConverter>
</UserControl.Resources>
Now you can bind the property Source
of the Image control like this: -
<Image Source="{Binding Level, Converter={StaticResource LevelToImage}}" />
Now that you invent new level numbers, you can add items to the converter.
By the way, you have it both inside TextBlock
and Image
out Button
. Button
can only contain one object, so you need to wrap those two inside some panel for example Grid
.
a source to share
Place the image in xaml and then bind its property Visibility
to the level using IValueConverter like this:
public class LevelToVisibilityConverter : IValueConverter
{
/// <exception cref="ArgumentException">TargetType must be Visibility</exception>
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if(!(value is int))
throw new ArgumentException("Source must be of type int");
if(targetType != typeof(Visibility))
throw new ArgumentException("TargetType must be Visibility");
int v = (int) value;
if (v == 1)
return Visibility.Visible;
else
return Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
a source to share