Referring to the document, we know that a property must be a dependency property to be the target of a {Binding} markup extension, but {x:Bind} does not have this requirement as it uses generated code to apply its binding values. From the code of Foreground
of DataGridTextColumn
, the Foreground
property is not a dependency property, therefore it is not supported to do the data binding by {Binding}
extension for the Foreground
property.
You could custom a CellTemplate
to set a data binding for the Foreground
property for a DataGridTemplateColumn instance.
For example:
<controls:DataGrid.Columns>
<controls:DataGridTemplateColumn Header="header">
<controls:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate x:DataType="local:Customer">
<StackPanel >
//MyBrush is a SolidColorBrush in Customer class, adjust it for your scenario
<TextBlock Text="{x:Bind FirstName}" Foreground="{x:Bind MyBrush}"/>
</StackPanel>
</DataTemplate>
</controls:DataGridTemplateColumn.CellEditingTemplate>
</controls:DataGridTemplateColumn>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…