MeuChapeu
MeuChapeu

Reputation: 226

change style of a single cell in a datagrid WPF / C#

code below sets a background color in a cell of datagrid, however when scrolling the grid. to scroll the grid values ​​are changing.

anyone know a solution?

foreach (var item in dgrid.SelectedItems)
            {
                var row = this.dgDados.ItemContainerGenerator.ContainerFromItem(dgDados) as DataGridRow;

                if (row == null)
                {
                    dgDados.UpdateLayout();
                    dgDados.ScrollIntoView(dgDados.Items.IndexOf(item));
                    row = (DataGridRow)dgDados.ItemContainerGenerator.ContainerFromIndex(dgDados.Items.IndexOf(item));
                }
                row.Background = corLinha;
            }

Upvotes: 0

Views: 1074

Answers (2)

Yuuki Wesp
Yuuki Wesp

Reputation: 1

<DataGridTextColumn Header="MyHeader" Binding="{Binding Path=MyPath, Converter={StaticResource MyConverter}" Width="Auto" IsReadOnly="True">
    <DataGridTextColumn.CellStyle>
        <Style TargetType="DataGridCell">
             <Setter Property="Background" Value="Yellow" />
        </Style>
    </DataGridTextColumn.CellStyle> 
</DataGridTextColumn>

Upvotes: 0

user1859022
user1859022

Reputation: 2705

in WPF you shouldn't do something like this in code - Use a Trigger in your XAML...
have a look at WPF DataGrid selected row style

Upvotes: 1

Related Questions