2009-12-04 37 views

Respuesta

36

Puede controlar el evento LoadingRow de DataGrid para detectar cuándo se agrega una fila. En el controlador de eventos, puede obtener una referencia al DataRow que se agregó a la DataTable que actúa como su ItemsSource. Luego puede actualizar el color de DataGridRow como quiera.

void dataGrid_LoadingRow(object sender, Microsoft.Windows.Controls.DataGridRowEventArgs e) 
{ 
    // Get the DataRow corresponding to the DataGridRow that is loading. 
    DataRowView item = e.Row.Item as DataRowView; 
    if (item != null) 
    { 
     DataRow row = item.Row; 

      // Access cell values values if needed... 
      // var colValue = row["ColumnName1]"; 
      // var colValue2 = row["ColumName2]"; 

     // Set the background color of the DataGrid row based on whatever data you like from 
     // the row. 
     e.Row.Background = new SolidColorBrush(Colors.BlanchedAlmond); 
    }   
} 

para inscribirse en el evento en XAML:

<toolkit:DataGrid x:Name="dataGrid" 
    ... 
    LoadingRow="dataGrid_LoadingRow"> 

O en C#:

this.dataGrid.LoadingRow += new EventHandler<Microsoft.Windows.Controls.DataGridRowEventArgs>(dataGrid_LoadingRow); 
+0

asegúrese de poner por defecto para las filas cuyo color no se desencadena por una condición –

+0

gracias. esa fue una manera increíblemente simple para mí. – Nasenbaer

+0

No funciona. el artículo siempre es nulo – Yusha

1

IMPORTANTE: asegúrese de asignar siempre los valores predeterminados para las filas que no están ser coloreado por una condición - o cualquier otro estilo.

Ver mi respuesta a C# Silverlight Datagrid - Row Color Change.

PS. Estoy en Silverlight y no han confirmado este comportamiento en WPF

10

U puede probar este

En el XAML

<Window.Resources> 
<Style TargetType="{x:Type DataGridRow}"> 
    <Style.Setters> 
     <Setter Property="Background" Value="{Binding Path=StatusColor}"></Setter> 
    </Style.Setters>    
</Style> 
</Window.Resources> 

En la cuadrícula de datos

<DataGrid AutoGenerateColumns="False" CanUserAddRows="False" Name="dtgTestColor" ItemsSource="{Binding}" > 
<DataGrid.Columns>        
    <DataGridTextColumn Header="Valor" Binding="{Binding Path=Valor}"/> 
</DataGrid.Columns> 
</DataGrid> 

En el código que tengo una clase con

public class ColorRenglon 
{ 
    public string Valor { get; set; } 
    public string StatusColor { get; set; } 
} 

Al establecer el DataContext

dtgTestColor.DataContext = ColorRenglon; 
dtgTestColor.Items.Refresh(); 

Si u no establecer el color de la fila el valor por defecto es gris

u puede tratar esta muestra con esta muestra

List<ColorRenglon> test = new List<ColorRenglon>(); 
ColorRenglon cambiandoColor = new ColorRenglon(); 
cambiandoColor.Valor = "Aqui va un color"; 
cambiandoColor.StatusColor = "Red"; 
test.Add(cambiandoColor); 
cambiandoColor = new ColorRenglon(); 
cambiandoColor.Valor = "Aqui va otro color"; 
cambiandoColor.StatusColor = "PaleGreen"; 
test.Add(cambiandoColor); 
Cuestiones relacionadas