Search code examples
wpfwpfdatagridwpftoolkit

Change DatagridCell Foreground while editing


How do I change the Foreground color while editing a Cell of a Datagrid? The Color should only be changed while editing.


Solution

  • Use EditingElementStyle on the column. Like this:

        <DataGrid ItemsSource="{Binding Items}" CanUserAddRows="True">
            <DataGrid.Columns>
                <DataGridTextColumn Header="col1" Binding="{Binding myString}">
                    <DataGridTextColumn.EditingElementStyle>
                        <Style TargetType="TextBox">
                            <Setter Property="Foreground" Value="Blue" />
                            <Setter Property="Background" Value="Yellow" />
                        </Style>
                    </DataGridTextColumn.EditingElementStyle>
                </DataGridTextColumn>
            </DataGrid.Columns>
        </DataGrid>