代码之家  ›  专栏  ›  技术社区  ›  ssg31415926

如何在代码中将Silverlight 3 DataGridCell置于编辑模式?

  •  6
  • ssg31415926  · 技术社区  · 16 年前

    我希望能够在Silverlight3.0数据网格中选择一个特定的单元格并将其置于编辑模式。我可以使用VisualTreeManager来定位单元格。如何切换到编辑模式?

    在VisualTreeManager中,每个DataGridCell都是这样的:

              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
    

    包含我要编辑的文本的文本块。

    更新

    根据@anthonywjones的建议,下面是我如何使用beginedit()来实现这一点。

    我想保持简单,所以我想在第一行选择一个列。就连我也不知道!最后,我通过创建一个名为first row的字段来保存第一行:

    private DataGridRow firstRow;
    

    向datagrid添加了loadingrow处理程序:

    LoadingRow="computersDataGrid_LoadingRow"
    

    和

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

    然后向面板添加按钮以触发编辑:

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

    我单击按钮,选择了正确的单元格,但它不会在单元格上进入编辑状态。这需要手动点击才能实现。

    2 回复  |  直到 14 年前
        1
  •  2
  •   AnthonyWJones    16 年前

    我不知道为什么需要使用VisualTreeManager找到DataGridCell,也不知道当前如何正确地开始编辑。只需将单元格的视觉状态设置为“编辑”即可。

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

    我不确定当你做上面的事情时网格是如何工作的。如果您需要datagrid来帮助您将更改还原到行中,您可能会发现有些事情是梨形的。

    “标准”方法是 DataGrid SelectedItem 属性设置为行所表示的项,设置 CurrrentColum 财产 DataGridColumn 对象,表示在其中找到单元格的列。然后打电话给 BeginEdit 方法。

        2
  •  0
  •   Manish Sinha    16 年前

    我不能很好地理解你的问题,但我也有类似的问题

    我只想使少数网格单元格可编辑,其余的则不可编辑。我没有创建逻辑并将readonly指定为true/false,而是做了一件简单的事情。

    • 标记整个网格的单元格是可写的, IsReadOnly 错误的
    • 设置事件 PreparingCellForEdit 回拨电话
    • 双击单元格时,它将进入编辑模式
    • 检查是否要编辑此单元格
    • 如果允许编辑,请继续
    • 如果该单元格是只读的,则调用 CancelEdit

    示例代码如下

    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;
        }
    }