问题描述
我目前正在处理一个DataGrid,在某些情况下,通过将 IsReadOnly
更改为true来禁用或启用特定的列,反之亦然。
我附加到 CurrentCellChanged
和 CellEditEnded
事件,其中我更改列 IsReadOnly
属性。
我期望应用程序在该列上禁用/启用编辑。
即使列有 IsReadOnly
设置为true有时它允许编辑。
我也试图在网格上调用 CancelEdit();
,但也没有任何影响。
如果您要求我可以发布代码,但我很确定逻辑是正确的,我检查它像数千次调试;)。
整个想法只不过是改变事件中特定列的IsReadOnly。
任何想法,为什么它不能像我预期的那样工作?
I'm currently working on a DataGrid which in some conditions should disable or enable particular columns by changing IsReadOnly
to true and vice versa.I attached to CurrentCellChanged
and CellEditEnded
events in which I change the column IsReadOnly
property.I expect the application to disable / enable edit on that column.Even though the column has IsReadOnly
set to true sometimes it does allow edits.I've also tried to call CancelEdit();
on a grid but that didn't make any effect either.If you request I can post code but I'm pretty sure the logic is fine, I checked it like thousands of times in debug ;).The entire idea is nothing more than changing IsReadOnly of particular column in event.Any idea what why it's not working as I expect?
Edit1。
添加代码
Edit1.Code added.
private void SrfDataGrid_CurrentCellChanged(object sender, EventArgs e)
{
CellCoordinates cellCoordinates = this.GetEditedCellCoordinates();
if (!this.LockDataGridCell(cellCoordinates))
{
if (!Keyboard.Modifiers.HasFlag(ModifierKeys.Control) && !Keyboard.Modifiers.HasFlag(ModifierKeys.Shift))
this.srfDataGrid.BeginEdit();
}
else
{
this.srfDataGrid.CancelEdit();
}
}
private void SrfDataGrid_CellEditEnded(object sender, DataGridCellEditEndedEventArgs e)
{
CellCoordinates cellCoordinates = this.GetEditedCellCoordinates();
this.SetCellsRowInfluence(cellCoordinates);
this.UnlockDataGridCell(cellCoordinates);
}
public bool LockDataGridCell(CellCoordinates cellCoordinates)
{
bool result = false;
if (cellCoordinates != null)
{
DataGridColumn currentColumn = this.srfDataGrid.CurrentColumn;
if (this.spreadSheetCellState[cellCoordinates.ColumnName, cellCoordinates.RowID].Equals(CurrentCellState.WRITE))
{
currentColumn.IsReadOnly = false;
}
else
{
currentColumn.IsReadOnly = true;
}
result = currentColumn.IsReadOnly;
}
return result;
}
public void UnlockDataGridCell(CellCoordinates cellCoordinates)
{
if (cellCoordinates != null)
{
DataGridColumn currentColumn = this.srfDataGrid.CurrentColumn;
if (this.spreadSheetCellState[cellCoordinates.ColumnName, cellCoordinates.RowID].Equals(CurrentCellState.ALWAYS_READ_ONLY))
{
currentColumn.IsReadOnly = true;
}
else
{
currentColumn.IsReadOnly = false;
}
}
}
推荐答案
尝试这样:
foreach (DataGridColumn col in dataGrid1.Columns)
{
if (col.GetType() == typeof(DataGridTextColumn))
{
col.IsReadOnly = true;
}
else
{
col.IsReadOnly = false;
}
}
这篇关于Datagrid列IsReadOnly属性在Silverlight 4中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!