问题描述
我想根据ist值更改wpf数据网格中单元格的背景.我的数据网格如下:
I would like to Change the Background of a cells in a wpf datagrid according to ist value. My datagrid is as follows:
<DataGrid x:Name="dataGridView_Comparison" ItemsSource="{Binding}" HorizontalAlignment="Stretch" AlternatingRowBackground="LightBlue" AlternationCount="2" Height="537" Margin="15,48,5,0" VerticalAlignment="Top" Width="1016" Background="#FF2C2727" Grid.ColumnSpan="2" Style="{DynamicResource DGHeaderStyle}" selectionUnit="FullRow">
我正在c#中动态创建此数据网格,因为列数始终根据输入而变化.做到这一点的代码部分是:
I am creating this datagrid dynamically in c# as the no of columns always vary depending on the Input. The Code part which does it is :
DataTable table = new DataTable();
table.Columns.Add("Column1", typeof(string));
foreach (string num in numbersList)
{
table.Columns.Add(num, typeof(string)); //Adding each numbers as a column in the table
}
foreach (string tc in uniqueCases)
{
DataRow newRow = table.NewRow();
newRow["Column1"] = tc+"_Case"; //Adding the case name of the row
foreach (string num in numbersList)
{
//For each number column add value corresponding to the condition..
newRow[num] = "0"; //Default value as 0
if (list_beforeThreshold.ContainsKey(num + tc) == true)
{
newRow[num] = "1";
}
if (list_afterThreshold.ContainsKey(num + tc) == true)
{
newRow[num] = "2";
}
table.Rows.Add(newRow);
}
dataGridView_Comparison.DataContext = table.DefaultView;//在wpf中添加到datagrid
dataGridView_Comparison.DataContext = table.DefaultView; //Adding to the datagrid in wpf
我在C#和WPF中还很陌生.有人可以指导我如何根据其值(0,1,2)为单元格提供不同的颜色.PS:我现在正在尝试数据触发.但没有任何进展.
I am fairly new in c# and wpf. Could someone please guide me on how to give differenet Colors to cell depending on their values(0,1,2).PS: I am trying datatrigger now. But not getting any Progress.
无法在xaml中对列号和列名进行硬编码,因为它们是在c#中动态填充的.
EDIT 1: The no of columns and the names of columns cannot be hardcoded in the xaml because they are dynamically populated in c#.
预先感谢
推荐答案
您可以使用 IValueConverter 更改背景颜色.
You can use IValueConverter to change the background color.
在xaml中定义Converter类:
<Window.Resources>
<local:NameToBrushConverter x:Key="NameToBrushConverter"/>
</Window.Resources>
<Grid>
<DataGrid x:Name="testDG" ItemsSource="{Binding tempData}">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Name}">
<DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Background" Value="{Binding Name, Converter={StaticResource NameToBrushConverter}}"/>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
转换器类别:
public class NameToBrushConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string input = value as string;
switch (input)
{
case "Smith":
return Brushes.LightGreen;
case "Willam":
return Brushes.LightPink;
default:
return DependencyProperty.UnsetValue;
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
} }
这篇关于如何根据值更改WPF数据网格中单元格的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!