Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,691 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hi,
I need to Change a Foreground Color in a DataGrid Line, here ist the XAML Code from the Line in the DataGrid:
<DataGridTemplateColumn Header="Nr " Width="38" CanUserSort="True" x:Name="SP001" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Label x:Name="SP001F" Height="50" Content="{Binding GrLaufendePosition}" Foreground="Black" Background="White" HorizontalAlignment="Center" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" BorderThickness="0" VerticalAlignment="Center" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
SP001.Foreground = "Red"; is wrong :-(
SP001F.Foreground = "Red"; is wrong too :-(
Have anyone a idea?
Best Regards
Bernd
There are many ways to do this, but the easier way is to refer with DynamicResource.
<DataGrid x:Name="dataGrid1" >
<DataGrid.Resources>
<SolidColorBrush Color="Black" x:Key="columnForeground"/>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTemplateColumn Header="Nr " Width="38" CanUserSort="True" x:Name="SP001" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate >
<Label x:Name="SP001F"
Height="50"
Content="{Binding GrLaufendePosition}"
Foreground="{DynamicResource columnForeground}"
Background="White"
HorizontalAlignment="Center"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
BorderThickness="0"
VerticalAlignment="Center"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
this.dataGrid1.Resources["columnForeground"] = Brushes.Red;
I found what i Need :-)
dg.RowBackground = Brushes.Red;
:-)))