public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
bind();
}
private void bind()
{
SampleData obj = new SampleData();
var m=obj.SampleData1();
dgClientIssueEntryMain.ItemsSource = m;
}
public class Person
{
public string Name { get; set; }
public int id { get; set; }
}
public class SampleData
{
public List<Person> SampleData1()
{
List<Person> people = new List<Person>();
people.Add(new Person() { Name = "Jack Sparrow" ,id=1});
people.Add(new Person() { Name = "William Turner", id = 2 });
people.Add(new Person() { Name = "Davi Jones", id = 3 });
people.Add(new Person() { Name = "Barbossa", id = 4 });
people.Add(new Person() { Name = "Maggie", id = 5 });
return people;
}
}
First I am creating one converter class
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string Flag = (string)value;
if (Flag == "Jack Sparrow")
{
return new SolidColorBrush(Colors.Green);
}
else if (Flag == "William Turner")
{
return new SolidColorBrush(Colors.Red);
}
else if (Flag == "Davi Jones")
{
return new SolidColorBrush(Colors.Purple);
}
else if (Flag == "Barbossa")
{
return new SolidColorBrush(Colors.Orange);
}
else
{
return new SolidColorBrush(Colors.Blue);
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
My XAML Page
1.First you need to add reference of converter in usercontrol.
xmlns:convert="clr-namespace:Usingconverter"
<UserControl.Resources>
<convert:converter x:Key="converter" ></convert:converter></UserControl.Resources>
2.Bind Data in Control and pass Converter to convert and get back Background Color.
<Grid x:Name="LayoutRoot" Background="White">
<sdk:DataGrid x:Name="dgClientIssueEntryMain" AutoGenerateColumns="False" VerticalAlignment="Center" IsReadOnly="True" HorizontalAlignment="Center"
Height="Auto" Width="Auto">
<sdk:DataGrid.Columns>
<sdk:DataGridTemplateColumn Width="35">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Canvas >
<Button x:Name="Rmv1" Background="{Binding Name, Converter={StaticResource converter}}" Width="30" Content="X" VerticalAlignment="Center" HorizontalAlignment="Center"></Button>
</Canvas>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
<sdk:DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
<sdk:DataGridTextColumn Header="id" Binding="{Binding id}"/>
</sdk:DataGrid.Columns>
</sdk:DataGrid>
</Grid>