禁用DataGrid中的特定单元格编辑

时间:2012-02-03 13:24:20

标签: c# silverlight datagrid

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

3 个答案:

答案 0 :(得分:2)

您可以将IsReadOnly属性用于此类

的特定单元格
 <DataGridTextColumn Header="ID"
                                        Binding="{Binding ID}"
                                        IsReadOnly="True"/>

我认为这是禁用特定单元格的最佳选择。 谢谢

答案 1 :(得分: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;
            }
        }
    }

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

答案 2 :(得分:0)

感谢NominSim,这也帮助我解决了我的问题,但是作为神经症 在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是一个更好的主意,但它对我没用。

相关问题