简体   繁体   中英

Datagrid column IsReadOnly property not working in Silverlight 4?

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

Try this:

foreach (DataGridColumn col in dataGrid1.Columns)
        {
            if (col.GetType() == typeof(DataGridTextColumn))
            {
                col.IsReadOnly = true;
            }
            else
            {
                col.IsReadOnly = false;
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM