简体   繁体   English

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

[英]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,而是做了一件简单的事情。

  • Mark the whole Grid's cells are writable, IsReadOnly as false 标记整个Grid的单元格是可写的, IsReadOnly为false
  • Set the event PreparingCellForEdit and send a callback 设置事件PreparingCellForEdit并发送回调
  • When you double click on a cell, it gets in the edit mode 双击单元格时,它将进入编辑模式
  • Check whether this cell you want to be editable 检查您希望此单元格是否可编辑
  • If it is allowed to be edited, go ahead 如果允许编辑,请继续
  • If that cell is ReadOnly, then call CancelEdit 如果该单元格是ReadOnly,则调用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.

相关问题 如何降低 Silverlight 中 DataGridCell 的高度? - How to reduce the height of a DataGridCell in Silverlight? 可以将Silverlight应用程序设置为Kiosk模式吗? - Can you put Silverlight app into Kiosk mode? 如何在Silverlight代码中确定运行Silverlight实例的位置? - How can I determine in silverlight code where the silverlight instance is running? 如何将Silverlight应用程序放入ASP MVC视图页面? - How can I put a Silverlight Application into a ASP MVC view page? 我可以在Silverlight 4中的RichTextBox中放置一个按钮吗? - Can I put a Button in a RichTextBox in Silverlight 4? 如何在Silverlight DataGrid中的编辑模式下更改单元格的文本颜色? - How to change text color of cell in edit mode in Silverlight DataGrid? 我该如何从中剪切和粘贴Silverlight只读编辑控件? - how to do I Silverlight read-only edit control, which I can cut and paste from? 如何获取有关Silverlight代码生成异常的详细信息? - How can I get details about a Silverlight code generation exception? Silverlight 应用; 如何将代码添加到按钮事件处理程序? - Silverlight Application; How can I add code to a buttons event handler? 如何将声音放入Silverlight动画中? - how I put sound in a silverlight animation?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM