[英]Datagrid column IsReadOnly property not working in Silverlight 4?
我目前正在研究一個DataGrid,在某些情況下應通過將IsReadOnly
更改為true來禁用或啟用特定的列,反之亦然。 我附加到CurrentCellChanged
和CellEditEnded
事件,在其中更改列IsReadOnly
屬性。 我希望應用程序在該列上禁用/啟用編輯。 即使該列的IsReadOnly
設置為true,有時它也允許編輯。 我也嘗試調用CancelEdit();
在網格上,但也沒有任何效果。 如果您要求我可以發布代碼,但是我很確定邏輯很好,那么我在調試中檢查了它數千次;)。 整個想法無非就是更改事件中特定列的IsReadOnly。 知道為什么它無法按我預期的那樣工作嗎?
編輯1。 代碼已添加。
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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.