2012-02-29 59 views
1

我想寫一個DataGridSeparatorColumn自定義控件,從DataGridColumn繼承,強制它是2像素寬,並有黑色背景。從DataGridColumn繼承的CustomControl:樣式問題

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代碼的示例,但我沒有找到任何有用的東西。任何人都可以用正確的方式指點我嗎

謝謝。

回答

0

試試這個:

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

PS:如果您希望列的標題也具有黑色,那麼您可以通過設置DataGridColumnHeader.BackgroundProperty和列的HeaderStyle來做類似的事情。 – 2012-02-29 09:48:03

+0

它適用於'HeaderStyle',但不適用於'CellStyle':標題單元具有預期的黑色背景,但單元格不是。 – 2012-02-29 10:12:43

+0

我想Eirik說,你也必須設置一個-2的邊距。否則,由於細胞太窄,無法看到黑色背景。 – 2012-02-29 10:21:11

1

bobsmith是在正確的軌道上,但是您需要調整保證金(也可能是填充)屬性的顏色覆蓋整個小區。

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可能不是您的情況的完美值,所以在這裏嘗試不同的值,直到您滿意爲止。

+0

那麼,我想說我的答案並不是要成爲OP的要求的完整解決方案,只是一個正確方向的指針; StackOverflow是關於指導和理解,而不是其他人爲你做你的工作!但無論如何,這足夠公平。 – 2012-02-29 10:07:33

+0

這兩個答案都很有用,順便說一句,我會把@ bobsmith833的標記作爲接受的標記 – 2012-02-29 11:05:50