I need to assign a color to the row I add at runtime to the DataTable. How can this be done?
我需要为运行时添加到DataTable的行分配颜色。这是怎么做到的呢?
3 个解决方案
#1
36
You can handle the DataGrid's LoadingRow event to detect when a row is being added. In the event handler you can get a reference to the DataRow that was added to the DataTable that is acting as your ItemsSource. Then you can update the DataGridRow's color however you like.
您可以处理DataGrid的LoadingRow事件,以检测何时添加了一行。在事件处理程序中,您可以获得对添加到充当ItemsSource的DataTable的DataRow的引用。然后可以更新DataGridRow的颜色。
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);
}
}
To sign up for the event in XAML:
报名参加XAML活动:
<toolkit:DataGrid x:Name="dataGrid"
...
LoadingRow="dataGrid_LoadingRow">
Or in C#:
或在c#中:
this.dataGrid.LoadingRow += new EventHandler<Microsoft.Windows.Controls.DataGridRowEventArgs>(dataGrid_LoadingRow);
#2
10
U can try this
你可以试试这个
In the XAML
在XAML
<Window.Resources>
<Style TargetType="{x:Type DataGridRow}">
<Style.Setters>
<Setter Property="Background" Value="{Binding Path=StatusColor}"></Setter>
</Style.Setters>
</Style>
</Window.Resources>
In the datagrid
在datagrid
<DataGrid AutoGenerateColumns="False" CanUserAddRows="False" Name="dtgTestColor" ItemsSource="{Binding}" >
<DataGrid.Columns>
<DataGridTextColumn Header="Valor" Binding="{Binding Path=Valor}"/>
</DataGrid.Columns>
</DataGrid>
In the code i have a class with
在代码中我有一个类
public class ColorRenglon
{
public string Valor { get; set; }
public string StatusColor { get; set; }
}
When set the DataContext
当设置DataContext
dtgTestColor.DataContext = ColorRenglon;
dtgTestColor.Items.Refresh();
If u not set the color of the row the default value is Grey
如果你不设置行的颜色,默认值是灰色的
u can try this sample with this sample
你可以试试这个样品
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);
#3
1
IMPORTANT : be sure to always assign defaults for rows that aren't being colored by a condition - or any other style.
重要:确保总是分配默认行不被条件——或任何其他颜色的风格。
See my answer to C# Silverlight Datagrid - Row Color Change.
参见我对c# Silverlight Datagrid行颜色更改的回答。
PS. I am in Silverlight and haven't confirmed this behavior in WPF
我在Silverlight中,还没有在WPF中确认这种行为
#1
36
You can handle the DataGrid's LoadingRow event to detect when a row is being added. In the event handler you can get a reference to the DataRow that was added to the DataTable that is acting as your ItemsSource. Then you can update the DataGridRow's color however you like.
您可以处理DataGrid的LoadingRow事件,以检测何时添加了一行。在事件处理程序中,您可以获得对添加到充当ItemsSource的DataTable的DataRow的引用。然后可以更新DataGridRow的颜色。
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);
}
}
To sign up for the event in XAML:
报名参加XAML活动:
<toolkit:DataGrid x:Name="dataGrid"
...
LoadingRow="dataGrid_LoadingRow">
Or in C#:
或在c#中:
this.dataGrid.LoadingRow += new EventHandler<Microsoft.Windows.Controls.DataGridRowEventArgs>(dataGrid_LoadingRow);
#2
10
U can try this
你可以试试这个
In the XAML
在XAML
<Window.Resources>
<Style TargetType="{x:Type DataGridRow}">
<Style.Setters>
<Setter Property="Background" Value="{Binding Path=StatusColor}"></Setter>
</Style.Setters>
</Style>
</Window.Resources>
In the datagrid
在datagrid
<DataGrid AutoGenerateColumns="False" CanUserAddRows="False" Name="dtgTestColor" ItemsSource="{Binding}" >
<DataGrid.Columns>
<DataGridTextColumn Header="Valor" Binding="{Binding Path=Valor}"/>
</DataGrid.Columns>
</DataGrid>
In the code i have a class with
在代码中我有一个类
public class ColorRenglon
{
public string Valor { get; set; }
public string StatusColor { get; set; }
}
When set the DataContext
当设置DataContext
dtgTestColor.DataContext = ColorRenglon;
dtgTestColor.Items.Refresh();
If u not set the color of the row the default value is Grey
如果你不设置行的颜色,默认值是灰色的
u can try this sample with this sample
你可以试试这个样品
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);
#3
1
IMPORTANT : be sure to always assign defaults for rows that aren't being colored by a condition - or any other style.
重要:确保总是分配默认行不被条件——或任何其他颜色的风格。
See my answer to C# Silverlight Datagrid - Row Color Change.
参见我对c# Silverlight Datagrid行颜色更改的回答。
PS. I am in Silverlight and haven't confirmed this behavior in WPF
我在Silverlight中,还没有在WPF中确认这种行为