Столбец Datagrid Свойство IsReadOnly не работает в Silverlight 4? - PullRequest
0 голосов
/ 16 февраля 2012

В настоящее время я работаю над DataGrid, который в некоторых условиях должен отключать или включать определенные столбцы, изменяя значение IsReadOnly на true и наоборот.Я прикрепил к CurrentCellChanged и CellEditEnded событиям, в которых я изменяю свойство столбца IsReadOnly.Я ожидаю, что приложение отключит / включит редактирование в этом столбце.Хотя для столбца IsReadOnly установлено значение true, иногда он разрешает редактирование.Я также пытался вызвать CancelEdit(); на сетке, но это тоже не дало никакого эффекта.Если вы спросите, я могу опубликовать код, но я уверен, что логика в порядке, я проверил это тысячи раз в отладке;).Вся идея - не более чем изменение IsReadOnly определенного столбца в событии.Любая идея, что, почему это не работает, как я ожидаю?

Edit1. Код добавлен.

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

1 Ответ

1 голос
/ 24 мая 2012

Попробуйте это:

foreach (DataGridColumn col in dataGrid1.Columns)
        {
            if (col.GetType() == typeof(DataGridTextColumn))
            {
                col.IsReadOnly = true;
            }
            else
            {
                col.IsReadOnly = false;
            }
        }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...