从DataGridColumn继承的CustomControl:样式问题

本文关键字:样式 问题 CustomControl DataGridColumn 继承 | 更新日期: 2023-09-27 18:26:10

我正试图编写一个从DataGridColumn继承的DataGridSeparatorColumn自定义控件,强制它为2像素宽并具有黑色背景。

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

我在谷歌上到处寻找TODO代码的示例,但没有发现任何有用的东西。有人能给我指对路吗?

谢谢。

从DataGridColumn继承的CustomControl:样式问题

bobsmith走在正确的轨道上,但您需要调整覆盖整个单元格的颜色的Margin(可能还有Padding)属性。

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可能不是您案例的完美值,所以在这里尝试不同的值,直到您满意为止。

试试这个:

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