gpt4 book ai didi

c# - 如何为 DataGrid 列单元格添加边框

转载 作者:太空宇宙 更新时间:2023-11-03 13:03:09 26 4
gpt4 key购买 nike

我在 WPF DataGrid 中显示数据,第一列显示静态文本“View”。我添加了下面的代码,使文本看起来像一个按钮。我不想做很多工作来创建按钮列自定义模板。这几乎可以工作了;文本居中,绘制边框。唯一不起作用的是背景没有出现——我仍然可以看到底层的交替行颜色。我还需要做些什么来激活背景颜色,还是因为 TextBlock 嵌套在 DataGridCell 和(我认为)其他一些对象中而出现问题?

[我也尝试过使用 TextBlock.BackgroundProperty 创建背景 setter ,但这也不起作用。我尝试将 BackgroundProperty 设置为 ImageBrush,这会更好,但它找不到我的图像位置。]

        private void dgvDetail_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
string sHeader = e.Column.Header.ToString();
if (sHeader.Trim().ToLower() == "view")
{
e.Column.CellStyle = GetViewColumnStyle();
}
}
private Style GetViewColumnStyle()
{
Style oStyle = new Style(typeof(DataGridCell));
Setter oTextAlignment = new Setter(TextBlock.TextAlignmentProperty, TextAlignment.Center);
oStyle.Setters.Add(oTextAlignment);
Setter oBackground = new Setter(DataGridCell.BackgroundProperty, Brushes.LightGray);
oStyle.Setters.Add(oBackground);
Setter oForeground = new Setter(DataGridCell.ForegroundProperty, Brushes.Black);
oStyle.Setters.Add(oForeground);
Setter oBorderBrush = new Setter(DataGridCell.BorderBrushProperty, Brushes.Black);
oStyle.Setters.Add(oBorderBrush);
Setter oMargin = new Setter(DataGridCell.MarginProperty, new Thickness(2, 2, 2, 2));
oStyle.Setters.Add(oMargin);
return oStyle;
}

最佳答案

您只对内部的 TextBlock 进行样式设置。您应该设置 DataGridCell.Template:

    private Style GetViewColumnStyle()
{
// The TextBlock
FrameworkElementFactory textBlockFactory = new FrameworkElementFactory(typeof(TextBlock));
// DataBinding for TextBlock.Text
Binding textBinding = new Binding("YourTextBindingPath");
textBlockFactory.SetValue(TextBlock.TextProperty, textBinding);
//Other TextBlock attributes
textBlockFactory.SetValue(TextBlock.TextAlignmentProperty, TextAlignment.Center);
textBlockFactory.SetValue(TextBlock.BackgroundProperty, Brushes.LightGray);
textBlockFactory.SetValue(TextBlock.ForegroundProperty, Brushes.Black);
textBlockFactory.SetValue(TextBlock.MarginProperty, new Thickness(2, 2, 2, 2));

// The Border around your TextBlock
FrameworkElementFactory borderFactory = new FrameworkElementFactory(typeof(Border));
borderFactory.SetValue(Border.BorderBrushProperty, Brushes.Black);
borderFactory.SetValue(Border.BorderThicknessProperty, new Thickness(1, 1, 1, 1));
// Add The TextBlock to the Border as a child element
borderFactory.AppendChild(textBlockFactory);

// The Template for each DataGridCell = your Border that contains your TextBlock
ControlTemplate cellTemplate = new ControlTemplate();
cellTemplate.VisualTree = borderFactory;

// Setting Style.Template
Style oStyle = new Style(typeof(DataGridCell));
Setter templateSetter = new Setter(DataGridCell.TemplateProperty, cellTemplate);
oStyle.Setters.Add(templateSetter);
return oStyle;
}

关于c# - 如何为 DataGrid 列单元格添加边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31755059/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com