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

前端 未结 2 1830
旧巷少年郎
旧巷少年郎 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: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(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;
        }
    }
    

提交回复
热议问题