Datagrid Row color based on Binding
Last post 04-17-2008 3:20 AM by Yi-Lun Luo - MSFT. 1 replies.
Sort Posts:
04-15-2008 5:40 PM
Datagrid Row color based on Binding

I have been implementing the Datagrid in one of my applications as an R&D excercise and I have run into a problem.

 Does anyone know if it is possible to set rows to have different colors based on a binding value. i.e. if the value is 0-100 then the row should be green, if the value is 101 - 500 the yellow, else red.

 I have tried many many different ideas, and converters, but cannot seem to affect only certain rows, I can only seem to be able to change all the rows(and/or alternating rows).

Any help or pointers would be appreciated.

Thanks

 Greg

g.duffield

Loading...
Joined on 03-16-2006
Posts 1
04-17-2008 3:20 AM
Marked as Answer
Re: Datagrid Row color based on Binding

Hello, you won't be able to set the background of the whole row. But you can set it on each cell. The downside is: You'll have to use DataGridTemplateColumn for all columns, and write all those templates. Here's a sample:

<UserControl.Resources>

<local:ColorConverter x:Key="cc"/>

</UserControl.Resources>

 

<Grid x:Name="LayoutRoot" Background="White">

<data:DataGrid x:Name="dg">

<data:DataGrid.Columns>

<data:DataGridTemplateColumn Header="Name">

<data:DataGridTemplateColumn.CellTemplate>

<DataTemplate>

<StackPanel Background="{Binding Color, Converter={StaticResource cc}}">

<TextBlock Text="Name"/>

</StackPanel>

</DataTemplate>

</data:DataGridTemplateColumn.CellTemplate>

</data:DataGridTemplateColumn>

 

<data:DataGridTemplateColumn Header="Age">

<data:DataGridTemplateColumn.CellTemplate>

<DataTemplate>

<StackPanel Background="{Binding Color, Converter={StaticResource cc}}">

<TextBlock Text="Age"/>

</StackPanel>

</DataTemplate>

</data:DataGridTemplateColumn.CellTemplate>

</data:DataGridTemplateColumn>

</data:DataGrid.Columns>

</data:DataGrid>

</Grid>

ObservableCollection<Data> source = new ObservableCollection<Data>();

source.Add(new Data() { Name = "Ared", Age = 23, Color = 600 });

source.Add(new Data() { Name = "Lante", Age = 22, Color = 10 });

source.Add(new Data() { Name = "Bright", Age = 21, Color = 200 });

this.dg.ItemsSource = source;

 

public class Data

{

public string Name { get; set; }

public int Age { get; set; }

public int Color { get; set; }

}

public class ColorConverter : IValueConverter

{

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

{

int source = (int)value;if (source <= 100)

{

return new SolidColorBrush(Colors.Green);

}

else if (source <= 500)

{

return new SolidColorBrush(Colors.Yellow);

}

return new SolidColorBrush(Colors.Red);

}

public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

{

throw new NotImplementedException();

}

}

 

shanaolanxing - Please mark the posts as answers if they help and unmark if they don't.

Yi-Lun Luo - MSFT

Loading...
Joined on 10-29-2007
Posts 2,066
Page view counter