我正在尝试根据条件为某些datagrids单元格着色,但不知道如何使用代码管理某些单元格的颜色
在以下代码中,如果日期为
,则第三列将为红色
我正在使用
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
LoadSummaryMesh();
}
private void LoadSummaryMesh()
{
DataTable dt = new DataTable();
dt.Columns.Add("c1");
dt.Columns.Add("c2");
dt.Columns.Add("xxx", typeof(DateTime));
dt.Rows.Add("aa", "" , DateTime.Now.AddDays(0));
dt.Rows.Add("bb", "" ,DateTime.Now.AddDays(1));
dt.Rows.Add("cc", "" , DateTime.Now.AddDays(2));
this.dataGrid1.DataContext = dt;
}
}
public class MyBkColorConverter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
DataRowView drv = value as DataRowView;
if (drv != null)
{
DateTime dt = DateTime.Parse(drv[2].ToString());
if (dt.Day % 2 == 0) //If it's a even number day.
return Brushes.Red;
}
return Brushes.White;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
和xaml
<Window x:Class="SummaryMesh.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:SummaryMesh"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:MyBkColorConverter x:Key="bkColorCvrt"/>
<Style x:Key="LeftAlignmentColumnHeaderStyle" TargetType="{x:Type DataGridColumnHeader}" BasedOn="{StaticResource WrappedColumnHeaderStyle}">
<Setter Property="HorizontalContentAlignment" Value="Left"/>
</Style>
<Style x:Key="soDataGrid_ColumnHeaderRotateStyle" TargetType="DataGridColumnHeader" >
<Setter Property="ContentTemplate" >
<Setter.Value>
<DataTemplate>
<TextBlock TextWrapping="Wrap" Text="{Binding}"
FontWeight="Bold" Width="90"
VerticalAlignment="Center" TextAlignment="Center"
HorizontalAlignment="Center">
<TextBlock.LayoutTransform>
<RotateTransform Angle="270" />
</TextBlock.LayoutTransform>
</TextBlock>
</DataTemplate>
</Setter.Value>
</Setter>
<Setter Property="HorizontalContentAlignment" Value="Center" />
</Style>
</Window.Resources>
<Grid>
<DataGrid ItemsSource="{Binding}"
AutoGenerateColumns="False"
ColumnHeaderStyle ="{StaticResource soDataGrid_ColumnHeaderRotateStyle}"
Margin="22,12,18,19" Name="dataGrid1"
xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding c1}" Width="80" Header="c1" />
<DataGridTextColumn Binding="{Binding c2}" Width="80" Header="c2"/>
<DataGridTextColumn Binding="{Binding xxx}" Width="80" Header="xxx">
<DataGridTextColumn.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="Background">
<Setter.Value>
<Binding Converter="{StaticResource bkColorCvrt}"/>
</Setter.Value>
</Setter>
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
获得:
现在的问题是如何才能将第二列标题颜色变为蓝色 以及如何将[第二列,第二行]设置为黄色(使用我正在使用的数据表)?
类似的东西答案 0 :(得分:1)
对于'黄色'单元格,您可以作为第三列。 添加新转换器:
public class MyBkColorConverter2 : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
DataRowView drv = value as DataRowView;
if (drv != null)
{
int rownum = drv.Row.Table.Rows.IndexOf(drv.Row);
if (rownum == 1)
{
return Brushes.Yellow;
}
}
return Brushes.White;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
在xaml中添加:
...
<local:MyBkColorConverter2 x:Key="bkColorCvrt2"/>
...
<DataGridTextColumn Binding="{Binding c2}" Width="80" Header="c2">
<DataGridTextColumn.HeaderStyle>
<Style TargetType="{x:Type DataGridColumnHeader}" BasedOn="{StaticResource soDataGrid_ColumnHeaderRotateStyle}">
<Setter Property="Background">
<Setter.Value>
<SolidColorBrush Color="Yellow"/>
</Setter.Value>
</Setter>
</Style>
</DataGridTextColumn.HeaderStyle>
<DataGridTextColumn.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="Background">
<Setter.Value>
<Binding Converter="{StaticResource bkColorCvrt2}"/>
</Setter.Value>
</Setter>
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>
对于列标题:
<DataGridTextColumn Binding="{Binding c2}" Width="80" Header="c2">
<DataGridTextColumn.HeaderStyle>
<Style TargetType="{x:Type DataGridColumnHeader}" BasedOn="{StaticResource soDataGrid_ColumnHeaderRotateStyle}">
<Setter Property="Background">
<Setter.Value>
<SolidColorBrush Color="Yellow"/>
</Setter.Value>
</Setter>
</Style>
</DataGridTextColumn.HeaderStyle>
</DataGridTextColumn>
答案 1 :(得分:0)
首先关于&#39;黄色&#39;您已经有一个答案,只需更改CellStyle
第二列,就像第三列一样。
第二个关于标题:在TextBlock
模板上为DataGridColumnHeader
添加样式,并设置所需的触发器,以便他们更改背景,或者将TextBlock
包裹在网格中并将其设置为&#39;基于触发器的背景。