views:

1164

answers:

2

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.

+1  A: 

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.

AnthonyWJones
I'd been led down this route by someone else. I'll have a go with what you've suggested and let you know. Thanks.
ssg31415926
I've tried both of your suggestions without success. First, the "standard" approach. Using the SelectedItem and CurrentColumn does result in the cell being highlighted but adding BeginEdit() has no effect. The cell doesn't get the focus and doesn't go into edit mode. Using the VisualStateManager didn't work, either.
ssg31415926
@ssg31415926 I suspected that the first approach wouldn't work, I'm surprised the second didn't though, can you edit your question to include a short amount of relevant code describing how you've tried it?
AnthonyWJones
I've updated the original post. Is it how you expected me to try it?
ssg31415926
@ssg31415926: Its close but you should be assigning an object from the actual source of data assigned to ItemsSource instead of trying to assign a DataGridRow object.
AnthonyWJones
The original source is a List<Thing> things and I'm now assigning this.dataGrid.SelectedItem = this.things[0]; but with the same result.
ssg31415926
I just solved my question thanks to the help here. Maybe this will work for you: http://stackoverflow.com/questions/2195938/how-do-i-immediately-validate-a-newly-inserted-row-in-a-silverlight-3-datagrid/2210453#2210453
Nick Gotch
A: 

I am not able to understand your problem properly, but I had a similar problem

I wanted to make only few of the Grid Cells editable and rest were not. Instead of creating a logic and assigning ReadOnly as true/ false, I did the simple thing.

  • Mark the whole Grid's cells are writable, IsReadOnly as false
  • Set the event PreparingCellForEdit and send a callback
  • When you double click on a cell, it gets in the edit mode
  • Check whether this cell you want to be editable
  • If it is allowed to be edited, go ahead
  • If that cell is ReadOnly, then call CancelEdit

The sample code goes 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;
    }
}
Manish Sinha