2013-05-13 54 views
1

我有當細胞被分配到下面定義的自定義類一個DataGrid:WPF DataGrid綁定細胞背景顏色分配數據的屬性對象

public class DataGridVariableWrapper : DependencyObject 
{ 
    public Variable TheVariable { get; set; } 

    public Brush BackgroundColor 
    { 
     get { return (Brush)GetValue(BackgroundColorProperty); } 
     set { SetValue(BackgroundColorProperty, value); } 
    } 
    public static readonly DependencyProperty BackgroundColorProperty = DependencyProperty.Register("BackgroundColor", typeof(Brush), typeof(DataGridVariableWrapper), new UIPropertyMetadata(null)); 

    public DataGridVariableWrapper(Brush backgroundBrush, Variable theVariable) 
    { 
     this.BackgroundColor = backgroundBrush; 
     this.TheVariable = theVariable; 
    } 

    public override string ToString() 
    { 
     return TheVariable.Value.ToString(); 
    } 

} 

我想有DataGridCell後臺綁定轉換爲此數據包裝類的BackgroundColor屬性。我試過了:

<DataGrid.CellStyle> 
    <Style TargetType="DataGridCell"> 
     <Setter Property="Background" Value="{Binding DataGridVariableWrapper.BackgroundColor}" /> 
    </Style> 
</DataGrid.CellStyle> 

但是背景顏色保持不變。我在這裏做錯了什麼?

+0

你試過'Value =「{Binding BackgroundColor}」'? – LPL 2013-05-13 17:51:54

+0

這似乎工作,只需要弄清楚顯示出來的小白色空間填充,而不是完整的背景填充。 – user2371475 2013-05-13 18:07:35

+0

明白了,必須將單元格的邊框厚度設置爲0.如果您想讓您的評論成爲答案,我會將其標記爲正確。 – user2371475 2013-05-13 18:12:58

回答

2

如果數據對象被分配到DataGridCell您可以在DataContext中找到它。這就是爲什麼你必須要做的綁定是爲了指定所需的屬性。

<Style TargetType="DataGridCell"> 
    <Setter Property="Background" Value="{Binding BackgroundColor}" /> 
</Style> 
相關問題