【问题标题】:How can I put a Silverlight 3 DataGridCell into edit mode in code?如何在代码中将 Silverlight 3 DataGridCell 置于编辑模式?
【发布时间】:2011-01-09 01:18:24
【问题描述】:

我希望能够选择 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();
}

我单击按钮并选择了正确的单元格,但它没有进入单元格上的编辑状态。需要手动点击才能实现。

【问题讨论】:

    标签: silverlight silverlight-3.0 datagrid


    【解决方案1】:

    我不确定您为什么需要使用 VisualTreeManager 找到 DataGridCell,我目前也不知道您将如何正确开始编辑。您可能只需将单元格的视觉状态设置为编辑即可。

     VisualStateManager.GoToState(myDataGridCell, "Editing", true);
    

    我不确定当您执行上述操作时网格的行为方式。如果您需要 DataGrid 来帮助您将更改恢复到一行,您可能会发现事情有点不顺利。

    “标准”方法是将DataGrid SelectedItem 属性设置为由行表示的项目,将CurrrentColum 属性设置为DataGridColumn 对象,该对象表示单元格所在的列成立。然后调用BeginEdit方法。

    【讨论】:

    • 我是被其他人带走的。我会按照你的建议去做,让你知道。谢谢。
    • 我已经尝试了您的两个建议,但均未成功。一是“标准”方法。使用 SelectedItem 和 CurrentColumn 确实会导致单元格被突出显示,但添加 BeginEdit() 没有效果。单元格没有获得焦点,也没有进入编辑模式。使用 VisualStateManager 也不起作用。
    • @ssg31415926 我怀疑第一种方法行不通,但我很惊讶第二种方法没有,您能否编辑您的问题以包含一小段相关代码来描述您的工作方式试过了吗?
    • 我已经更新了原帖。这是你希望我尝试的方式吗?
    • @ssg31415926:它很接近,但您应该从分配给 ItemsSource 的实际数据源中分配一个对象,而不是尝试分配一个 DataGridRow 对象。
    【解决方案2】:

    我无法正确理解您的问题,但我遇到了类似的问题

    我只想让少数网格单元格可编辑,其余的则不可编辑。我没有创建逻辑并将 ReadOnly 分配为 true/false,而是做了简单的事情。

    • 将整个 Grid 的单元格标记为可写,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;
        }
    }
    

    【讨论】:

    • @neurotix 它不起作用?真的吗?它对我有用。 Silverlight 3. 我认为这不会改变 SL4
    • 它对我根本没有任何影响:(。我也尝试通过更改 IsReadOnly Dinamically 来实现与您在此处所做的相同的事情,但这也会随机失败。如果您想签出:@ 987654321@
    猜你喜欢
    • 2011-05-27
    • 1970-01-01
    • 1970-01-01
    • 2010-12-20
    • 2019-12-06
    • 2016-12-11
    • 2011-02-05
    • 1970-01-01
    • 2010-09-17
    相关资源
    最近更新 更多