Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I want to be able to pick a specific cell in a Silverlight 3.0 DataGrid and put it into edit mode. I can use the VisualTreeManager to locate the cell. How do I switch to edit mode?

Each DataGridCell looks like this in the 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 the TextBlock containing the text I want to edit.

Update

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

I wanted to keep it simple so I thought I'd pick a column in the first row. Even that proved beyond my SL knowledge! In the end, I get the first row by creating a field called firstRow to hold it:

private DataGridRow firstRow;

added a LoadingRow handler to the 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 trigger the edit:

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

I click the button and the correct cell is selected but it doesn't go into edit on the cell. It takes a manual click to achieve that.

like image 298
ssg31415926 Avatar asked Jan 25 '10 10:01

ssg31415926


1 Answers

I'm not sure why you need to find the DataGridCell using VisualTreeManager nor do I know currently how you would properly start editing . You may get away with simply setting the cell's visual state to editing.

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

I'm not sure how the grid behaves when you do something like the above. You may find things goe a bit pearshaped if you need DataGrid to help you revert changes to a row.

The "standard" approach would be to set the DataGrid SelectedItem property to the item represented by the row, set the CurrrentColum property to the DataGridColumn object that represents to the column in which the cell is found. Then call the BeginEdit method.

like image 145
AnthonyWJones Avatar answered Oct 08 '22 02:10

AnthonyWJones