How can I put Silverlight 3 DataGridCell in edit mode in code?

I want to be able to select a specific cell in Silverlight 3.0 DataGrid and put it in edit mode. I can use VisualTreeManager to search for a cell. How to switch to edit mode?

Each DataGridCell looks like this in VisualTreeManager:

System.Windows.Controls.DataGridCell System.Windows.Controls.Grid System.Windows.Shapes.Rectangle System.Windows.Controls.ContentPresenter System.Windows.Controls.TextBlock System.Windows.Shapes.Rectangle System.Windows.Shapes.Rectangle 

with a TextBlock containing the text I want to change.

Update

Following @AnthonyWJones suggestion, here is how I tried to do this using BeginEdit ().

I wanted it to be simple, so I decided to select the column in the first row. Even this turned out to be above my knowledge of SL! In the end, I get the first line by creating a firstRow field to store it:

 private DataGridRow firstRow; 

Added LoadRow handler in DataGrid:

 LoadingRow="computersDataGrid_LoadingRow" 

and

 private void computersDataGrid_LoadingRow(object sender, DataGridRowEventArgs e) { if (this.firstRow == null) this.firstRow = e.Row; } 

and then adding a button to the panel to start editing:

 private void Button_Click(object sender, RoutedEventArgs e) { this.dataGrid.SelectedItem = this.firstRow; this.dataGrid.CurrentColumn = this.dataGrid.Columns[4]; this.dataGrid.BeginEdit(); } 

I press the button and the correct cell is selected, but it does not go into editing on the cell. This requires a manual click.

+6
silverlight datagrid
source share
2 answers

I'm not sure why you need to find a DataGridCell using VisualTreeManager, and I also don't know how to start editing correctly. You can leave by simply setting the visual state of the cell for editing.

  VisualStateManager.GoToState(myDataGridCell, "Editing", true); 

I'm not sure how the grid works when you do something like the above. You might find something a little punchier if you need a DataGrid to help you roll back the changes to the row.

A β€œstandard” approach would be to set the DataGrid SelectedItem property to the element represented by this row, set the CurrrentColum property to the DataGridColumn object, which represents the column in which the cell is found. Then call the BeginEdit method.

+2
source share

I cannot correctly understand your problem, but I had a similar problem.

I wanted to make only some of the editable mesh elements, but there was no rest. Instead of creating logic and designating ReadOnly as true / false, I did a simple thing.

  • Mark all grid cells for recording, IsReadOnly as false
  • Set the PreparingCellForEdit event and send the callback
  • When you double-click on a cell, it goes into edit mode
  • Check if this cell should be editable.
  • If editing is allowed, continue
  • If this cell is ReadOnly, then call CancelEdit

Sample code looks like

 namespace foo { public class foobar { public foobar() { sampleGrid = new DataGrid(); sampleGrid.IsReadOnly = false; sampleGrid.PreparingCellForEdit += new EventHandler<DataGridPreparingCellForEditEventArgs>(sampleGrid_PreparingCellForEdit); } void sampleGrid_PreparingCellForEdit(object sender, DataGridsampleGrid_PreparingCellForEditEventArgs e) { if (sampleGrid.SelectedItem != null) { bool isWritableField = CheckIfWritable() if (isWritableField == false) { sampleGrid.CancelEdit(); } // continue with your logic } } private DataGrid sampleGrid; } } 
0
source share

All Articles