如何在代码中将Silverlight 3 DataGridCell置于编辑模式?
我希望能够在Silverlight 3.0数据网格中选择一个特定的单元格,并将其置于编辑模式。我可以使用VisualTreeManager定位单元格。如何切换到编辑模式 VisualTreeManager中的每个DataGridCell如下所示:如何在代码中将Silverlight 3 DataGridCell置于编辑模式?,silverlight,silverlight-3.0,datagrid,Silverlight,Silverlight 3.0,Datagrid,我希望能够在Silverlight 3.0数据网格中选择一个特定的单元格,并将其置于编辑模式。我可以使用VisualTreeManager定位单元格。如何切换到编辑模式 VisualTreeManager中的每个DataGridCell如下所示: System.Windows.Controls.DataGridCell System.Windows.Controls.Grid System.Windows.Shapes.Re
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()实现这一点的
我想保持简单,所以我想在第一行选择一列。就连这也超出了我的知识范围!最后,我通过创建名为firstRow的字段来保存第一行:
private DataGridRow firstRow;
向DataGrid添加了LoadingRow处理程序:
LoadingRow="computersDataGrid_LoadingRow"
及
然后将按钮添加到面板以触发编辑:
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
属性设置为行所表示的项,将currentcolm
属性设置为表示找到单元格的列的DataGridColumn
对象。然后调用BeginEdit
方法。我无法正确理解您的问题,但我遇到了类似的问题
我只想使其中的几个网格单元可编辑,其余的则不可编辑。我没有创建逻辑并将ReadOnly指定为true/false,而是做了一件简单的事情
- 将整个网格的单元格标记为可写,
为falseIsReadOnly
- 设置事件
并发送回调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;
}
}
名称空间foo
{
公共级foobar
{
公共食品
{
sampleGrid=newdatagrid();
sampleGrid.IsReadOnly=false;
sampleGrid.PreparingCellForEdit+=新事件处理程序(sampleGrid\u PreparingCellForEdit);
}
void sampleGrid\u PreparingCellForEdit(对象发送方,DataGridsampleGrid\u PreparingCellForEditEventArgs e)
{
if(sampleGrid.SelectedItem!=null)
{
bool iswriteablefield=checkifwriteable()
if(isWritableField==false)
{
sampleGrid.CancelEdit();
}
//继续你的逻辑
}
}
私有数据网格采样网格;
}
}
我是被其他人领着走这条路的。我会同意你的建议,并让你知道。谢谢。你的两个建议我都试过了,但都没有成功。第一,“标准”方法。使用SelectedItem和CurrentColumn确实会导致单元格高亮显示,但添加BeginEdit()无效。单元格没有获得焦点,也没有进入编辑模式。使用VisualStateManager也不起作用。@ssg31415926我怀疑第一种方法不起作用,但我很惊讶第二种方法不起作用,你能编辑你的问题,包括一小段描述你是如何尝试的相关代码吗?我已经更新了原始帖子。这就是您希望我尝试的方式吗?@ssg31415926:很接近,但您应该从分配给ItemsSource的实际数据源分配对象,而不是试图分配DataGridRow对象。@Neurtix它不起作用?真正地对我来说是的。银光3号。我不认为SL4应该改变这一点,它对我没有任何影响:(.我也尝试了与您在这里做的相同的事情,仅通过友好方式更改IsReadOnly,但这也随机失败。如果您愿意签出:
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;
}
}