【发布时间】:2013-05-13 17:13:30
【问题描述】:
我有一个 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>
但背景颜色保持不变。我在这里做错了吗?
【问题讨论】:
-
你试过
Value="{Binding BackgroundColor}"吗? -
这似乎奏效了,只需要弄清楚显示的小空白填充而不是完整的背景填充。
-
知道了,必须将单元格的边框厚度设置为 0。如果您想让您的评论成为答案,我会将其标记为正确。
-
我已经发布了我的评论作为答案。谢谢。