我希望能够选择 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();
}
我点击按钮后,正确的单元格被选中,但它没有进入单元格编辑状态。需要手动点击才能实现。