WPF Datagrid单元格着色

时间:2015-12-17 05:22:35

标签: wpf datagrid

我们如何在WPF数据网格中以编程方式为单元格添加颜色?我可以向行添加颜色,但我想从后面的代码中添加颜色到单元格。请为此提供代码吗?

3 个答案:

答案 0 :(得分:1)

此代码的作用:首先,您处理LoadingRow事件,获取行(它具有保存绑定项的Item属性),获取绑定项,进行所有需要的计算,获取单元格你需要改变并最终改变目标细胞的风格。

这是代码(作为项目,我使用了一个带有用于着色的int ID属性的示例对象。):

<强> C#:

private void FillTheDataGrid()
{
    List<SomeClass> list = new List<SomeClass>();
    Random rnd = new Random();
    for (int i = 0; i < 10; i++)
    {        
       list.Add(new SomeClass() { DaysOld = DateTime.Now.Add(new TimeSpan(rnd.Next(5), 0, 0, 0)), ID=i});
     }
     dataGrid.ItemsSource = list;
}

private void dataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
{
    Dispatcher.BeginInvoke(DispatcherPriority.Render, new Action(() => AlterRow(e)));
}

private void AlterRow(DataGridRowEventArgs e)
{
    var cell = GetCell(dataGrid, e.Row, 1);
    if (cell == null) return;

    var item = e.Row.Item as SomeClass;
    if (item == null) return;

    var value = item.ID;

    if (value <= 1) cell.Background = Brushes.Red;
    else if (value <= 2) cell.Background = Brushes.Yellow;
    else cell.Background = Brushes.Green;
}

public static DataGridRow GetRow(DataGrid grid, int index)
{
    var row = grid.ItemContainerGenerator.ContainerFromIndex(index) as DataGridRow;

    if (row == null)
    {
        // may be virtualized, bring into view and try again
        grid.ScrollIntoView(grid.Items[index]);
        row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
    }
    return row;
}

public static T GetVisualChild<T>(Visual parent) where T : Visual
{
    T child = default(T);
    int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
    for (int i = 0; i < numVisuals; i++)
    {
        var v = (Visual)VisualTreeHelper.GetChild(parent, i);
        child = v as T ?? GetVisualChild<T>(v);
        if (child != null)
        {
            break;
        }
    }
    return child;
}

public static DataGridCell GetCell(DataGrid host, DataGridRow row, int columnIndex)
{
    if (row == null) return null;

    var presenter = GetVisualChild<DataGridCellsPresenter>(row);
    if (presenter == null) return null;

    // try to get the cell but it may possibly be virtualized
    var cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex);
    if (cell == null)
    {
        // now try to bring into view and retreive the cell
        host.ScrollIntoView(row, host.Columns[columnIndex]);
        cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex);
    }
    return cell;
}

<强>型号:

public class SomeClass
{
    public int ID { get; set; }
    public DateTime DaysOld { get; set; }
}

<强> XAML:

<DataGrid Name="dataGrid" AutoGenerateColumns="True" LoadingRow="dataGrid_LoadingRow"/>

答案 1 :(得分:0)

根据DataGridCell中的内容,您可以创建一个绑定颜色或使用触发器的DataTemplate

<DataTemplate DataType="{x:Type cell:MyCellObject}"> 
    <TextBlock TextAlignment="Center" Text="{Binding Text}">
        <TextBlock.Style>
            <Style TargetType="TextBlock">
                <Setter Property="Background" Value="{Binding MyBackground}" />
                <Setter Property="Foreground" Value="Black" />
                <Setter Property="Padding" Value="0" />

                <Style.Triggers>    
                    <DataTrigger Binding="{Binding MyBool}" Value="True">
                        <Setter Property="Foreground" Value="DarkRed" />
                        <Setter Property="FontWeight" Value="Bold" />
                    </DataTrigger>
                </Style.Triggers>

           </Style>
        </TextBlock.Style>
    </TextBlock>
</DataTemplate>

答案 2 :(得分:0)

  1. 我遇到了同样的问题,最后我找到了答案,并且比我想的要容易,这是通过编程方式更改单元格颜色的代码:

      

    DataGridRow firstRow =   dataGrid.ItemContainerGenerator.ContainerFromItem(dataGrid.Items [i])   作为DataGridRow; firstColumnInFirstRow =   dataGrid.Columns [j] .GetCellContent(firstRow).Parent作为DataGridCell;   firstColumnInFirstRow.Background = Brushes.IndianRed;