[英]How to make WPF datagrid cell ReadOnly for certain rows?
I use a WPF data grid to edit a database table. 我使用WPF数据网格来编辑数据库表。 The table contains several records with 16 columns (properties).
该表包含具有16列(属性)的几条记录。
I want to make a column read only if the user tries to edit it when one of the columns of the selected record has a specific value (in other words, disable editing of that particular column for certain records). 我希望仅当选定记录的某一列具有特定值时用户尝试对其进行编辑的情况下,才能使该列为只读(换句话说,对于某些记录,禁用该特定列的编辑)。
I was thinking to bind to column IsReadOnly property, but I do not know how to pass the column which I need to verify (or at least the current row) as a converter parameter. 我当时想绑定到列IsReadOnly属性,但是我不知道如何将需要验证的列(或至少当前行)作为转换器参数传递。
Does anybody have an idea how to do this? 有人知道如何执行此操作吗?
I found a solution here. 我在这里找到了解决方案。 Using the DataGrid.BeginningEdit event to conditionally check if the cell is editable and then set the Cancel property on the event args if not.
使用DataGrid.BeginningEdit事件有条件地检查单元格是否可编辑,然后在事件args上设置Cancel属性(如果不是)。
Conditionally making readonly to WPF DataGridCell 有条件地使WPF DataGridCell只读
I don't know if it's the best solution, but it works. 我不知道这是否是最好的解决方案,但是它有效。
I suggest making the specific DataColumn
Readonly, while setting the ItemsSource
of the DataGrid
. 我建议在设置
DataGrid
的ItemsSource
的同时使特定的DataColumn
Readonly。 Consider the following as an example: 考虑以下示例:
DataTable tab = new DataTable();
DataColumn col = tab.Columns.Add("a");
// data added code
foreach (DataColumn col in tab.Columns)
foreach (DataRow r in tab.Rows)
{
if (r[col].Equals("..."))
{
col.ReadOnly = true;
break;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.