How To Add A Border To DataGrid Column Cells

时间:2015-07-31 20:56:46

标签: c# wpf datagrid

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

1 个答案:

答案 0 :(得分:1)

你只是在里面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;
    }
相关问题