Note that there are some explanatory texts on larger screens.

plurals
  1. POSilverlight DataGrid BeginEdit method causes CellEditEnded event
    text
    copied!<p>I have a requirement that users should be able to click on an (editable) cell in a grid, and it goes into edit-mode immediately rather than having to double-click on press F2. To do this I added a behavior:</p> <pre><code>public class AutomaticallyEditGridBehavior : Behavior&lt;DataGrid&gt; { protected override void OnAttached() { base.OnAttached(); if (AssociatedObject.IsReadOnly) { return; } AssociatedObject.CurrentCellChanged += OnCurrentCellChanged; } private void OnCurrentCellChanged(object sender, EventArgs e) { // Check our column is not readonly first if (AssociatedObject.CurrentColumn.IsReadOnly) { return; } AssociatedObject.BeginEdit(); } } </code></pre> <p>I also have some logic for when the cell has been edited, so I added another behavior:</p> <pre><code>public class CellEditEndedBehavior : Behavior&lt;DataGrid&gt; { protected override void OnAttached() { base.OnAttached(); AssociatedObject.CellEditEnded += CellEditEnded; } private void CellEditEnded(object sender, DataGridCellEditEndedEventArgs e) { if (e.EditAction != DataGridEditAction.Commit) { return; } if (AssociatedObject.SelectedItem == null) { return; } // Some processing and logic to get next editable item // Set the focus to the next item and begin editing AssociatedObject.SelectedItem = nextEditableItem; AssociatedObject.CurrentColumn = e.Column; AssociatedObject.Focus(); AssociatedObject.BeginEdit(); } } </code></pre> <p>I find that when I click on a cell, the first behavior runs, but before there is a chance to edit that cell the CellEditEnded event has fired. Therefore, the focus moves to the next object and not the cell that was just clicked on. </p> <p>Why is this event firing before the cell has actually been edited? Is there a better way to do this?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload