perplexedDev
perplexedDev

Reputation: 835

AlternatingRowBackground color changes on datagrid edit

I have a datagrid in which i set the AlternatingRowBackground.I allow the users to edit the values of the datagrid. But when the user edits values, the background color changes to white.

<DataGrid Name="grdTests" AlternatingRowBackground="#C4E6FF"/>

I have not been able to find out why it changes, I dont set the backgroundcolor anywhere else.

Upvotes: 0

Views: 143

Answers (1)

monstr
monstr

Reputation: 1730

It is because White - is default color for editing mode and AlternatingRowBackground only for viewing mode. You can use this approach:

<Style TargetType="{x:Type DataGridCell}">
    <Style.Triggers>
        <Trigger Property="IsEditing" Value="True">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="DataGridCell">
                        <TextBox Text="{Binding RelativeSource={RelativeSource TemplatedParent}, 
                                    Path=Content.Text, Mode=TwoWay, UpdateSourceTrigger=Default}"
                                    HorizontalAlignment="Stretch" 
                                    VerticalAlignment="Stretch" 
                                    Padding="0" 
                                    BorderThickness="0" 
                                    Background="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=AlternatingRowBackground}"/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Trigger>
    </Style.Triggers>
</Style>

Put this style in Resources collection of DataGrid and then, color of editing cells will be like your AlternatingRowBackground color.

Upvotes: 1

Related Questions