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?
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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.