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

C# 禁用DataGrid中的特定单元格编辑,c#,silverlight,datagrid,C#,Silverlight,Datagrid,我需要知道是否可以禁用DataGrid中的特定单元格编辑,而不禁用Silverlight 4中整个列的编辑。我可以将特定单元格对象作为FrameworkElement获取,但它不包含属性IsReadOnly或IsEnabled。 你可能会问:为什么我需要它?我的应用程序需要根据其他单元格内容禁用行中的特定单元格。每一行都以这种方式分别进行检查。 如果你知道我如何能做出如此不寻常的行为,请写信;) 如果有要禁用的单元格的行、列索引: int r = 2, c = 4; 然后,您可以收听CellE

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

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

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;
            }
        }
    }

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

谢谢诺明西姆,这也帮我解决了我的问题,但作为神经质的人 在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;     
    }
}
启用编辑功能

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();  

    }

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

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

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

我认为这是禁用特定单元的最佳方法。
谢谢

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