简体   繁体   English

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

[英]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. 我目前正在研究一个DataGrid,在某些情况下应通过将IsReadOnly更改为true来禁用或启用特定的列,反之亦然。 I attached to CurrentCellChanged and CellEditEnded events in which I change the column IsReadOnly property. 我附加到CurrentCellChangedCellEditEnded事件,在其中更改列IsReadOnly属性。 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. 即使该列的IsReadOnly设置为true,有时它也允许编辑。 I've also tried to call CancelEdit(); 我也尝试调用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. 整个想法无非就是更改事件中特定列的IsReadOnly。 Any idea what why it's not working as I expect? 知道为什么它无法按我预期的那样工作吗?

Edit1. 编辑1。 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;
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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