Reputation: 15
I've got a DataGrid where the cells are assigned to a custom class defined below:
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();
}
}
I'm trying to have the DataGridCell background bound to the BackgroundColor property of this data wrapper class. I've tried:
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="Background" Value="{Binding DataGridVariableWrapper.BackgroundColor}" />
</Style>
</DataGrid.CellStyle>
But the background color remains unchanged. Am I doing something wrong here?
Upvotes: 1
Views: 5758
Reputation: 17083
If a data object is assigned to a DataGridCell
you will find it in the DataContext
. That's why all you have to do in binding is to specify the desired property.
<Style TargetType="DataGridCell">
<Setter Property="Background" Value="{Binding BackgroundColor}" />
</Style>
Upvotes: 2