Good time of day, how to change the color of the column of a selected cell in datagrid WPF
1 answer
You can do this by placing the style for the selected cell into the resources and subscribe to CurrentCellChanged :
<DataGrid ItemsSource="{Binding Items}" CurrentCellChanged="DataGrid_CurrentCellChanged"> <DataGrid.Resources> <Style TargetType="DataGridCell" x:Key="SelectedColumnStyle"> <Setter Property="Background" Value="Red"/> </Style> </DataGrid.Resources> </DataGrid> In the subscriber, just apply the style to the current column:
DataGridColumn CurrentColumn = null; private void DataGrid_CurrentCellChanged(object sender, EventArgs e) { var dataGrid = (DataGrid)sender; if (CurrentColumn != null) CurrentColumn.CellStyle = null; CurrentColumn = dataGrid.CurrentColumn; if (CurrentColumn != null) CurrentColumn.CellStyle = (Style)dataGrid.Resources["SelectedColumnStyle"]; } |
