2013-05-13 30 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> 
相关问题