【问题标题】:Datagrid column IsReadOnly property not working in Silverlight 4?Datagrid 列 IsReadOnly 属性在 Silverlight 4 中不起作用?
【发布时间】:2012-02-16 12:19:14
【问题描述】:

我目前正在开发一个 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;
            }
        }
    }

【问题讨论】:

  • 请附上代码,我们讨厌文字!
  • 好吧,我附上了最重要的代码

标签: c# silverlight-4.0 datagrid


【解决方案1】:

试试这个:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-15
    • 1970-01-01
    • 2011-02-02
    • 2011-07-19
    相关资源
    最近更新 更多