Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何向DataGrid列单元格添加边框_C#_Wpf_Datagrid - Fatal编程技术网

C# 如何向DataGrid列单元格添加边框

C# 如何向DataGrid列单元格添加边框,c#,wpf,datagrid,C#,Wpf,Datagrid,我在WPF数据网格中显示数据,第一列显示静态文本“视图”。我添加了下面的代码,使文本看起来像一个按钮。我不想做很多工作来创建一个按钮列自定义模板。这几乎是可行的;文本居中,绘制边框。唯一不起作用的是背景没有出现——我仍然可以看到下面交替的行颜色。我还需要做些什么来激活背景色,还是因为TextBlock嵌套在DataGridCell和(我认为)一些其他对象中而出现问题 [我还尝试使用TextBlock.BackgroundProperty创建背景设置器,但这也不起作用。我尝试将Background

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

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

private void dgvDetail_AutoGeneratingColumn(对象发送方,DataGridAutoGeneratingColumnEventArgs e)
{
string sHeader=e.Column.Header.ToString();
if(sHeader.Trim().ToLower()=“视图”)
{
e、 Column.CellStyle=GetViewColumnStyle();
}
}
私有样式GetViewColumnStyle()
{
Style-oStyle=新样式(typeof(DataGridCell));
Setter oTextAlignment=新的Setter(TextBlock.TextAlignmentProperty,TextAlignment.Center);
oStyle.Setters.Add(oTextAlignment);
Setter oBackground=新Setter(DataGridCell.BackgroundProperty,bruses.LightGray);
oStyle.Setters.Add(oBackground);
重磨设置器=新设置器(DataGridCell.ForegroundProperty,Bruss.Black);
添加(再研磨);
Setter-oBorderBrush=新的Setter(DataGridCell.borderbrush属性,brush.Black);
oStyle.Setters.Add(oBorderBrush);
Setter oMargin=新Setter(DataGridCell.MarginProperty,新厚度(2,2,2,2));
oStyle.Setters.Add(奥马尔金);
回归式;

}
您只需在内部设置
文本块的样式。您应该设置
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;
    }

巴赫曼·艾利斯,非常感谢。这在第一次使用时效果很好。我所要做的就是更改绑定路径值。非常令人印象深刻。DataGrid样式对我来说非常复杂,但也许我可以通过研究您的解决方案获得一些理解。我想将您的解决方案标记为答案,但这似乎不是StackOverflow的工作方式。