繁体   English   中英

Datagrid列的IsReadOnly属性在Silverlight 4中不起作用?

[英]Datagrid column IsReadOnly property not working in Silverlight 4?

我目前正在研究一个DataGrid,在某些情况下应通过将IsReadOnly更改为true来禁用或启用特定的列,反之亦然。 我附加到CurrentCellChangedCellEditEnded事件,在其中更改列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.

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