如何让DataGrid列背景透明?

问题描述 投票:0回答:1

我希望在 WPF 应用程序中的 DataGrid 中有一个具有透明背景的列。标题和单元格本身需要透明,但其中的内容不透明,这将是一个按钮。

我看到 RowBackground 存在,但没有列的参数。

c# wpf xaml datagrid
1个回答
0
投票

假设这是 Windows 窗体中的 C#,它需要一个

CellPainting
函数将网格着色为透明,然后替换数据。全部在标准 .NET 库中完成。这是一个例子: Data with first column transparent

这是函数:

` private void DataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
 {
     if (e.ColumnIndex == 0 && e.RowIndex == -1)
     {
         e.Graphics.FillRectangle(new SolidBrush(Color.Transparent), e.CellBounds);
         e.PaintContent(e.CellBounds);
         e.Handled = true;
     }
     else if (e.ColumnIndex == 0 && e.RowIndex >= 0)
     {

         e.Graphics.FillRectangle(new SolidBrush(Color.Transparent), e.CellBounds);
         e.PaintContent(e.CellBounds);
         e.Handled = true;
     }
 }`

初始化网格后放置此行:

dataGridView1.CellPainting += DataGridView1_CellPainting;

© www.soinside.com 2019 - 2024. All rights reserved.