[英]How can I put a Silverlight 3 DataGridCell into edit mode in code?
我希望能够在Silverlight 3.0 DataGrid中选择一个特定的单元格并将其置于编辑模式。 我可以使用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();
}
我单击按钮并选择了正确的单元格,但它不会在单元格上进行编辑。 需要手动点击才能实现这一目标。
我不确定为什么你需要使用VisualTreeManager找到DataGridCell,我也不知道你现在如何正确地开始编辑。 您可以简单地将单元格的视觉状态设置为编辑。
VisualStateManager.GoToState(myDataGridCell, "Editing", true);
当你做上面这样的事情时,我不确定网格是如何表现的。 如果您需要DataGrid来帮助您将更改还原到某行,您可能会发现有点梨形状。
“标准”方法是将DataGrid
SelectedItem
属性设置为行所表示的项,将CurrrentColum
属性设置为DataGridColumn
对象,该对象表示找到该单元格的列。 然后调用BeginEdit
方法。
我无法正确理解您的问题,但我遇到了类似的问题
我想让只有少数网格单元可编辑而其余部分则不可编辑。 我没有创建逻辑并将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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.