【问题标题】:Disabling specific cell edit in DataGrid禁用 DataGrid 中的特定单元格编辑
【发布时间】:2012-02-03 13:24:20
【问题描述】:

我需要知道是否可以禁用 DataGrid 中的特定单元格编辑,无需禁用 Silverlight 4 中整个列的编辑。我可以将特定单元格对象作为 FrameworkElement 获取,但它不包含属性 IsReadOnly 或 IsEnabled。 你可能会问:我为什么需要它?好吧,我的应用程序需要根据其他单元格内容禁用行中的特定单元格。以这种方式分别检查每一行。 如果您知道我如何实现这种不寻常的行为,请写信;)

【问题讨论】:

    标签: c# silverlight datagrid


    【解决方案1】:

    如果您有要禁用的单元格的行、列索引:

    int r = 2, c = 4;
    

    然后您可以监听 CellEnter 和 CellLeave 事件并执行以下操作:

        private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == r)
            {
                if (e.ColumnIndex == c)
                {
                    dataGridView1.Columns[e.ColumnIndex].ReadOnly = true;
                }
            }
        }
    
        private void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == r)
            {
                if (e.ColumnIndex == c)
                {
                    dataGridView1.Columns[e.ColumnIndex].ReadOnly = false;
                }
            }
        }
    

    您仍在将整个列设置为只读,但由于您在离开单元格后将其重置回来,因此其效果似乎只适用于该单元格。

    【讨论】:

    • 我使用了一些不同的方法。我连接了 DataGrid 的 CurrentCellChanged 事件和 CellEditEnded 事件。但这个想法真的很有帮助。谢谢!
    • 你不知道什么是布尔逻辑,是吗?
    • if(true){if(true){ x = y;}}这样的代码是不好的做法。它的写法类似于if(true && true){ x = y; }
    • @Nordvind 我写代码的目的是为了 OP,而不是写最好的代码。这个问题相当简单,我不想用任何 OP 可能 不理解的东西来混淆答案。如果您在 SO 的答案中寻找“完美”代码,那么您来错地方了。本网站旨在作为一种学习工具。
    • >我像教 OP 不要编写最好的代码一样编写代码所以你做到了 - 你教 OP 如何编写“不是最好的”代码。假设初学者应该写难以阅读的废话只是愚蠢的,更不用说。如果一个人在做 OOP 并且不知道布尔逻辑是什么(在学校教过),他/她应该改变他/她的职业。
    【解决方案2】:

    您可以像这样对特定单元格使用 IsReadOnly 属性

     <DataGridTextColumn Header="ID"
                                            Binding="{Binding ID}"
                                            IsReadOnly="True"/>
    

    这是禁用特定单元格的最佳方法。 谢谢

    【讨论】:

      【解决方案3】:

      感谢 NominSim,这也帮助我解决了我的问题,但作为 Neurotix 在 SilverLight 4 中的 DataGrid 上没有找到 CellEnter 和 CellLeave 方法。

      正如 NominSim 所说,您需要知道行和列的索引。

      我是怎么解决的:

      禁用编辑

      System.Windows.Threading.DispatcherTimer timMakeEditable = new System.Windows.Threading.DispatcherTimer();
      
        private void dataGrid1_PreparingCellForEdit(object sender, DataGridPreparingCellForEditEventArgs e)
      {
          timMakeEditable.Interval = new TimeSpan(0, 0, 0, 0, 100); // 100 Milliseconds 
          timMakeEditable.Tick += new EventHandler(timer_Tick);
          timMakeEditable.Start();
      
          if (e.RowIndex == r && e.ColumnIndex == c)
          {
                  dataGrid1.Columns[yourColumnIndex].IsReadOnly = true;     
          }
      }
      

      启用编辑

      几毫秒后,计时器使列启用:

      void timer_Tick(object sender, EventArgs e)
          {
              dataGrid1.Columns[yourColumnIndex].IsReadOnly = false;
              timMakeEditable.Stop();  
      
          }
      

      我认为使用 cellEditEnded 是一个更好的主意,但它对我不起作用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-05-16
        • 2011-10-09
        • 2017-02-17
        • 1970-01-01
        • 2013-04-10
        • 2011-12-11
        • 2017-07-25
        相关资源
        最近更新 更多