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

6

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

每个 DataGridCell 在 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。

更新

根据@AnthonyWJones的建议,这是我尝试使用BeginEdit()来完成此操作的方式。

我想保持简单,所以我想选择第一行中的一列。甚至那也超出了我的SL知识范围!最终,我通过创建一个名为firstRow的字段来获取第一行:

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个回答

2

我不确定为什么您需要使用VisualTreeManager查找DataGridCell,也不知道如何正确地开始编辑。您可以尝试仅将单元格的可视状态设置为编辑状态。

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

我不确定当你执行类似上述操作时,网格会如何表现。如果你需要DataGrid帮助你还原对行所做的更改,你可能会发现事情有些混乱。
“标准”方法是将DataGrid的SelectedItem属性设置为该行表示的项,将CurrrentColum属性设置为表示单元格所在列的DataGridColumn对象。然后调用BeginEdit方法。

我被别人引导走了这条路。我会尝试你建议的方法并告诉你结果。谢谢。 - ssg31415926
我尝试了你提出的两个建议,但都没有成功。首先,使用“标准”方法。使用SelectedItem和CurrentColumn确实会突出显示单元格,但添加BeginEdit()没有任何效果。单元格没有获得焦点,也没有进入编辑模式。使用VisualStateManager也不起作用。 - ssg31415926
@ssg31415926 我怀疑第一种方法行不通,但我很惊讶第二种方法也不行,你能否编辑你的问题并包含一小段相关代码来描述你如何尝试它? - AnthonyWJones
我已更新原帖。这是你期望我尝试的方式吗? - ssg31415926
原始源是一个 List<Thing> things,我现在将其分配为 this.dataGrid.SelectedItem = this.things[0],但结果相同。 - ssg31415926
显示剩余2条评论

0

我无法完全理解你的问题,但我曾经遇到过类似的问题。

我想让网格中的某些单元格可编辑,而其他单元格则不可编辑。我没有创建逻辑并将ReadOnly设置为true/false,而是采取了简单的方法。

  • 将整个网格的单元格标记为可写入,IsReadOnly为false
  • 设置事件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;
    }
}

@neurotix 不工作?真的吗?对我来说可行。Silverlight 3。我认为这不应该改变为SL4。 - Manish Sinha
对我来说完全没有任何效果:(。我也尝试通过动态更改IsReadOnly来实现与您在此处所做的相同的事情,但这也会随机失败。如果您有兴趣查看: http://stackoverflow.com/questions/9311158/datagrid-column-isreadonly-property-not-working-in-silverlight-4 - mike

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接