silentman.it
silentman.it

Reputation: 361

CustomControl inherited from DataGridColumn: problems with styling

I'm trying to write a DataGridSeparatorColumn custom control, inherited from DataGridColumn, forcing it to be 2 pixels wide and have a black background.

public class DataGridSeparatorColumn : DataGridColumn
{
    public DataGridSeparatorColumn()
    {
        CanUserReorder = false;
        CanUserResize = false;
        CanUserSort = false;

        MaxWidth = 2;
        MinWidth = 2;

        IsReadOnly = true;

        Header = "";

        // TODO: Set black background and/or other visual stuff here                

    }

    protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem)
    {
        //return new FrameworkElement();
        return null;
    }

    protected override FrameworkElement GenerateEditingElement(DataGridCell cell, object dataItem)
    {
        //return new FrameworkElement();
        return null;
    }
}

I googled all around to find a sample for the TODO-code but I havent found anything useful. Can anyone point me the right way?

Thank you.

Upvotes: 1

Views: 357

Answers (2)

Eirik
Eirik

Reputation: 4215

bobsmith is on the right track, but you need to adjust the Margin (and possibly Padding) properties for the color cover the entire cell.

Style style = new Style(typeof(DataGridCell));
style.Setters.Add(new Setter(DataGridCell.BackgroundProperty, new SolidColorBrush(Colors.Black)));
style.Setters.Add(new Setter(DataGridCell.MarginProperty, new Thickness(-2.0)));

CellStyle = style;

-2.0 might not be the perfect value for your case, so try different values here until you're satisfied.

Upvotes: 1

Stephen Holt
Stephen Holt

Reputation: 2368

Try this:

Style myStyle = new Style();
Setter myBlackBackgroundSetter = new Setter();
myBlackBackgroundSetter.Property = DataGridCell.BackgroundProperty;
myBlackBackgroundSetter.Value = Brushes.Black;
myStyle.Setters.Add(myBlackBackgroundSetter);
CellStyle = myStyle;

Upvotes: 0

Related Questions