Reputation: 5
I'm trying to change the Background
property for the currently selected cell when I double clicked it, but since I'm new to WPF I got some issues. I have tried this way with XAML:
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Style.Triggers>
<Trigger Property="IsFocused" Value="True">
<Setter Property="Background" Value="#FF333333"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
And programmatically:
private void DataMapping_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
var row = sender as DataGridRow;
row.Background = new SolidColorBrush(Color.FromRgb(51, 51, 51));
}
Any suggestions? Full code for the Datagrid here.
Upvotes: 0
Views: 1791
Reputation: 22079
You have to create and set an EditingElementStyle
for your data grid column, because you are in edit-mode when you double click a cell. In that mode, the data grid cell contains specific controls for editing, like a TextBox
for text columns, so changing the cell background will not have an effect.
The editing style below sets the Background
and Foreground
of the TextBox
in edit-mode.
<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding DataGridRows}" ...>
<DataGrid.Resources>
<!-- ...other data grid resources. -->
<Style x:Key="DataGridTextColumnEditingStyle"
TargetType="{x:Type TextBox}"
BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="Background" Value="#FF333333"/>
<Setter Property="Foreground" Value="White"/>
</Style>
</DataGrid.Resources>
<!-- ...other data grid code. -->
<DataGrid.Columns>
<!-- ...other data grid columns -->
<DataGridTextColumn Header="CSV Column"
IsReadOnly="False"
Binding="{Binding Path=CSVColumnValue}"
Width="*"
Foreground="White"
EditingElementStyle="{StaticResource DataGridTextColumnEditingStyle}"/>
</DataGrid.Columns>
</DataGrid>
Upvotes: 1