如何在代码中将Silverlight 3 DataGridCell设置为编辑模式? [英] How can I put a Silverlight 3 DataGridCell into edit mode in code?

查看:103
本文介绍了如何在代码中将Silverlight 3 DataGridCell设置为编辑模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够在Silverlight 3.0 DataGrid中选择一个特定的单元格,并将其放入编辑模式。我可以使用VisualTreeManager来定位单元格。如何切换到编辑模式?

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?

每个DataGridCell在VisualTreeManager中如下所示:

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

,其中TextBlock包含我要编辑的文本。

with the TextBlock containing the text I want to edit.

更新

以下是@AnthonyWJones的建议,以下是我如何使用BeginEdit()进行操作。

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

我想保持简单,所以我以为我'在第一行选择一列。即使这已经超出了我的SL知识!最后,我通过创建一个名为firstRow的字段来获取第一行:

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;

向DataGrid添加了一个LoadRow处理程序:

added a LoadingRow handler to the DataGrid:

LoadingRow="computersDataGrid_LoadingRow"

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.

推荐答案

我不知道为什么你需要使用VisualTreeManager找到DataGridCell我现在知道你如何正确地开始编辑。您可能只需将单元格的视觉状态设置为编辑。

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

当您执行上述操作时,我不知道网格的行为。如果您需要DataGrid帮助您将更改还原到一行,您可能会发现有些东西有点珍珠。

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.

标准方法将设置 DataGrid SelectedItem 属性到该行表示的项目,设置 CurrrentColum 属性到代表查找单元格的列的 DataGridColumn 对象。然后调用 BeginEdit 方法。

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.

这篇关于如何在代码中将Silverlight 3 DataGridCell设置为编辑模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆