简体   繁体   中英

How To Add A Border To DataGrid Column Cells

I am displaying data in a WPF DataGrid and the first column shows the static text "View". I added the code below to make the text look like a button. I don't want to do a lot of work to create a button column custom template. This is almost working; the text is centered, the border is drawn. The only thing not working is the background does not appear - I can still see the underlying alternating row colors. Is there something else I need to do to activate the background color, or does the problem arise because the TextBlock is nested inside the DataGridCell and (I think) some other objects?

[I've also tried creating the Background setter with TextBlock.BackgroundProperty and that doesn't work either. And I tried setting BackgroundProperty to an ImageBrush which would be even better, but it couldn't find my image location.]

 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; }

You are only styling the TextBlock inside. You should set 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;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM