如何设置WPF datagrid的单元格颜色?

Stevenzhu18 2019-05-14 03:41:18
Hi,小弟在开发一个WPF软件,其中有一个datagrid,这个datagrid的数据源是绑定datatable,datatable的数据是从数据来的。

datagrid显示表格里面,有一列是实际测试值,另一列是标准值,现在是对比实测值和标准值,如果在标准内就黑色,如果不在标准内就红色,比如达到下图的效果:



谢谢
...全文
1103 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
RunnerDNA 2019-05-23
  • 打赏
  • 举报
回复
把DataGridCell模板样式里的水平样式设置去掉,就是你要的效果:
RunnerDNA 2019-05-23
  • 打赏
  • 举报
回复
实现效果:


数据源里设一个只读属性用来改变字体颜色:

public class DataGridItem
{
public DataGridItem(int test, int standard)
{
_test = test;
_standard = standard;
}

private int _test;
private int _standard;

public int Test
{
get
{
return _test;
}

set
{
_test = value;
}
}

public int Standard
{
get
{
return _standard;
}

set
{
_standard = value;

}
}

public Brush SetColor
{
get
{
if (_test >= _standard)
return Brushes.Red;
else
return Brushes.Black;
}
}
}


窗体代码:
<Window x:Class="DataGridDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:DataGridDemo"
mc:Ignorable="d" SizeToContent="Height"
Title="MainWindow" Width="500" Loaded="Window_Loaded">
<Window.Resources>
<Style TargetType="DataGridColumnHeader">
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="Width" Value="100" />
<Setter Property="Height" Value="26" />
<Setter Property="FontWeight" Value="Normal"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="DataGridColumnHeader">
<Border x:Name="BackgroundBorder" Width="Auto" Background="Yellow">
<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<ContentPresenter Margin="0,0,0,0" VerticalAlignment="Center" HorizontalAlignment="Center"/>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="{x:Type DataGridRow}">
<Setter Property="Background" Value="White" />
<Setter Property="Height" Value="20"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="White" />
</Trigger>
</Style.Triggers>
</Style>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="White" />
<Setter Property="BorderBrush" Value="Transparent" />
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid Margin="20">
<DataGrid Name="datagrid" IsReadOnly="True" Width="200" Height="auto" Background="White" BorderBrush="Gray" HeadersVisibility="Column"
AutoGenerateColumns="False" CanUserAddRows="False" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"
ScrollViewer.CanContentScroll="False" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Disabled">
<DataGrid.Columns>
<DataGridTemplateColumn Width="100" Header="实测" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Test}" Foreground="{Binding Path=SetColor}" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Width="100" Header="标准" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Standard}" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>

数据加载:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
ObservableCollection<DataGridItem> itemList = new ObservableCollection<DataGridItem>();
itemList.Add(new DataGridItem(95, 96));
itemList.Add(new DataGridItem(96, 93));
itemList.Add(new DataGridItem(92, 96));
itemList.Add(new DataGridItem(94, 96));
itemList.Add(new DataGridItem(95, 91));
this.datagrid.ItemsSource = itemList;
}

E次奥 2019-05-16
  • 打赏
  • 举报
回复
根据你的需求,给模板设置DataTrigger,根据不同类型的值显示不同字体颜色;
only_youlix 2019-05-15
  • 打赏
  • 举报
回复
要么写个转换器,要么数据源绑定成List<model>,里面加个brush属性 在get里面 判断一下 返回对应的颜色,界面直接绑定 ,我一直用的list,很少直接帮datatable
exception92 2019-05-14
  • 打赏
  • 举报
回复
groveer 2019-05-14
  • 打赏
  • 举报
回复
datatable新增一列bool型,后台判断 在标准内为true 否则为false 然后自定义个转换器转换成brush xaml中Foreground绑定这一列就行
在Windows系统中,notepad.exe(记事本)是一个“经典的”、“简洁的”文本编辑器。这个软件,没有华丽的外观,也没有繁杂的功能,仅仅是一个文本编辑小软件。虽然经过Windows系统数十年的变换,但它却保持着永恒姿态,数十年来几乎不曾改变过。曾经,VS中的经典DEMO中,就有它的身影,一个新建的项目,就藏有一个新建的“记事本”。然而,在WPF的项目中,“记事本”却消失的无影无踪,也许是很容易实现,也许是为了革新,而不愿再传承“经典”。确实,使用WPF技术再次让“记事本”复活,确实也是一件非常容易的事情。但是,如果,使用WPF技术,再搭配当下非常流行的MVVM模式呢?复活“记事本”的难度却陡然上升至很多WPF程序员为之默默叹气。而,MVVM模式是掌握WPF的最顶级技术,MVVM模式拥有的无尽的优势,让WPF相对于过往的编程模式来说,是一种革命性的创新,从而也成为大中型WPF项目中必须的模式。但,学习难度。。。。。。在这个《WPF记事本开发详解》的课程中,赵老师带领你在WPF中,从零开始一步步构建MVVM模式,直到让你亲自以WPF+MVVM的方式,让这个经典的“记事本”软件从你的手中“复活”。在课程中,赵老师会详细讲解WPF和MVVM中的各种技巧,让你从此爱上WPF+MVVM编程。

8,735

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧