How to change background color of a cell when it is double clicked?

1.4k views Asked by At

Code:

<Grid>
    <telerik:RadGridView x:Name="HierarchialGridView" AutoGenerateColumns="False">
        <telerik:RadGridView.Columns>
            <telerik:GridViewDataColumn DataMemberBinding="{Binding Id}" Header="Id" />
            <telerik:GridViewDataColumn DataMemberBinding="{Binding Name}" Header="Name" />
        </telerik:RadGridView.Columns>
    </telerik:RadGridView>
</Grid>

Now default white color is displaying after double clicking a cell. How to achieve to change the background color of a cell when it is double clicked in XAML code ?

1

There are 1 answers

1
Alberto On BEST ANSWER

If you want to do it in xaml I think that you need at least to re-template the grid or the cell.

But if you are ok with a little code-behind:

  • Add and handler for the MouseDoubleClick event for the RadGrid

and then:

private void RadGridView_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
     FrameworkElement originalSender = e.OriginalSource as FrameworkElement;
     if (originalSender != null)
     {
          var cell = originalSender.ParentOfType<GridViewCell>();
          if (cell != null)
          {
              cell.Background = new SolidColorBrush(Colors.Red);
          }
     }
}

Edit:

Subscribe to the PreparedCellForEdit event and:

private void RadGridView_PreparedCellForEdit(object sender, GridViewPreparingCellForEditEventArgs e)
{
    var editingControl = e.EditingElement as Control;
    if(editingControl !=null)
       editingControl.Background = new SolidColorBrush(Colors.Red);

}