Reputation: 621
I have a Datagrid (DataGridMeterValues) in my C# wpf application. Everytime the user exits the editing mode of a cell, the CellEditEnding event is triggered.
Now I wish to change the background of the cell when the event is triggered.
This is what I got so far:
private void DataGridMeterValues_CellEditEnding(object sender, System.Windows.Controls.DataGridCellEditEndingEventArgs e)
{
// Code to change background color here
}
I am able to change the background of a full row with following code:
e.Row.Background = Brushes.Yellow;
Now my question is, can I do the same but then only for 1 cell (the selected one) not the entire row
EDIT: This is the XAML for the datagrid
<DataGrid Grid.Row="3" Grid.Column="1" AutoGenerateColumns="False" Name="DataGridMeterValues" ItemsSource="{Binding Path=MeterValuesList, UpdateSourceTrigger=PropertyChanged}" AlternatingRowBackground="LightGray" BorderBrush="Gray" BorderThickness="1" FrozenColumnCount="0" CanUserResizeColumns="False" CanUserResizeRows="False" Margin="2,0" CanUserSortColumns="False" SelectionMode="Single" CanUserReorderColumns="False" CanUserAddRows="False" CanUserDeleteRows="False" HeadersVisibility="Column" GridLinesVisibility="All" AreRowDetailsFrozen="False" IsEnabled="True" CellEditEnding="DataGridMeterValues_CellEditEnding">
<DataGrid.Resources>
<Style x:Key="DataGridBase" TargetType="Control">
<Setter Property="LayoutTransform">
<Setter.Value>
<TransformGroup>
<RotateTransform Angle="-90" />
<ScaleTransform ScaleX="1" ScaleY="-1" />
</TransformGroup>
</Setter.Value>
</Setter>
<Setter Property="TextOptions.TextFormattingMode" Value="Display" />
</Style >
<Style TargetType="DataGridCell" BasedOn="{StaticResource DataGridBase}"/>
<Style TargetType="DataGridColumnHeader" BasedOn="{StaticResource DataGridBase}"/>
</DataGrid.Resources>
<DataGrid.LayoutTransform>
<TransformGroup>
<RotateTransform Angle="90" />
<MatrixTransform Matrix="-1, 0, 0, 1, 0, 0" />
</TransformGroup>
</DataGrid.LayoutTransform>
<DataGrid.RowHeaderStyle >
<Style TargetType="DataGridRowHeader">
<Setter Property="Content" Value="X" />
</Style>
</DataGrid.RowHeaderStyle>
<DataGrid.Columns>
<DataGridTemplateColumn Header="Action" >
<DataGridTemplateColumn.CellTemplate >
<DataTemplate>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<Button Tag="{Binding}" Content="Activate" Width="50" BorderThickness="0" Margin="2" Background="{Binding Path=Activated}" Click="BtnActivate_Click" />
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Name" Binding="{Binding Path=Title, UpdateSourceTrigger=PropertyChanged}" />
<DataGridTextColumn Header="Meter Type" Binding="{Binding Path=Type, UpdateSourceTrigger=PropertyChanged}" />
</DataGrid.Columns>
</DataGrid>
Upvotes: 3
Views: 7320
Reputation: 2074
private void DataGridMeterValues_CellEditEnding(object sender, System.Windows.Controls.DataGridCellEditEndingEventArgs e)
{
FrameworkElement element = e.Column.GetCellContent(DataGridMeterValues.SelectedItem);
(element.Parent as DataGridCell).Background = new SolidColorBrush(Colors.Red);
}
I have fetched the framework element of the cell in selected row and column. Then I got DataGridCell from that element using its Parent property and set its background property. :)
There can be multiple templates inside a DataGridCell. Its better we fetched the DataGridCell and used its background property.
Upvotes: 6