[英]How can I put a Silverlight 3 DataGridCell into edit mode in code?
I want to be able to pick a specific cell in a Silverlight 3.0 DataGrid and put it into edit mode. 我希望能够在Silverlight 3.0 DataGrid中选择一个特定的单元格并将其置于编辑模式。 I can use the VisualTreeManager to locate the cell.
我可以使用VisualTreeManager来定位单元格。 How do I switch to edit mode?
如何切换到编辑模式?
Each DataGridCell looks like this in the 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
with the TextBlock containing the text I want to edit. 使用包含我要编辑的文本的TextBlock。
Update 更新
Following @AnthonyWJones' suggestion, here's how I tried to do this using BeginEdit(). 按照@AnthonyWJones的建议,这是我尝试使用BeginEdit()的方法。
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!
即使这证明超出了我的SL知识! In the end, I get the first row by creating a field called firstRow to hold it:
最后,我通过创建一个名为firstRow的字段来保存第一行:
private DataGridRow firstRow;
added a LoadingRow handler to the DataGrid: 向DataGrid添加了一个LoadingRow处理程序:
LoadingRow="computersDataGrid_LoadingRow"
and 和
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.
需要手动点击才能实现这一目标。
I'm not sure why you need to find the DataGridCell using VisualTreeManager nor do I know currently how you would properly start editing . 我不确定为什么你需要使用VisualTreeManager找到DataGridCell,我也不知道你现在如何正确地开始编辑。 You may get away with simply setting the cell's visual state to editing.
您可以简单地将单元格的视觉状态设置为编辑。
VisualStateManager.GoToState(myDataGridCell, "Editing", true);
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来帮助您将更改还原到某行,您可能会发现有点梨形状。
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. “标准”方法是将
DataGrid
SelectedItem
属性设置为行所表示的项,将CurrrentColum
属性设置为DataGridColumn
对象,该对象表示找到该单元格的列。 Then call the BeginEdit
method. 然后调用
BeginEdit
方法。
I am not able to understand your problem properly, but I had a similar problem 我无法正确理解您的问题,但我遇到了类似的问题
I wanted to make only few of the Grid Cells editable and rest were not. 我想让只有少数网格单元可编辑而其余部分则不可编辑。 Instead of creating a logic and assigning ReadOnly as true/ false, I did the simple thing.
我没有创建逻辑并将ReadOnly指定为true / false,而是做了一件简单的事情。
IsReadOnly
as false IsReadOnly
为false PreparingCellForEdit
and send a callback PreparingCellForEdit
并发送回调 CancelEdit
CancelEdit
The sample code goes like 示例代码如下
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.