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

前端 未结 2 1831
旧巷少年郎
旧巷少年郎 2021-01-18 14:26

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 e

相关标签:
2条回答
  • 2021-01-18 14:54

    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.

    0 讨论(0)
  • 2021-01-18 14:58

    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;
        }
    }
    
    0 讨论(0)
提交回复
热议问题