0

DataGridsを使用してWPFプロジェクトに取り組んでいます。ユーザーが必要な数の行を選択できるようにするか、単一のセルのみを選択できるようにします。つまり、セル範囲の選択を無効にします。しかし、私はそれをすることができませんでした。

これは可能ですか?

私は次のコードを試しました:

public MyDataGrid : DataGrid
{
    public ExtendedDataGrid()
    {
        SelectionMode = DataGridSelectionMode.Extended;
        SelectionUnit = DataGridSelectionUnit.CellOrRowHeader;
        this.SelectedCellsChanged += new SelectedCellsChangedEventHandler(MyDataGrid_SelectedCellsChanged);
    }

    void MyDataGrid_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
    {
        if (this.SelectedCells.Count > 1)
        {                
            DataGridCellInfo currentCell = this.CurrentCell;
            this.SelectedCells.Clear();
            this.CurrentCell = currentCell;
        }
    }

しかし、このコードでは、行全体を選択することはできません。

それで、必要な数の行を選択するが、ユーザーがセル範囲を選択できないようにする方法はありますか?

前もって感謝します。

4

1 に答える 1

1

私は自分の問題を解決したと思います:

private void MyDataGrid_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
    {
        int columnsCount = this.Columns.Count;
        int selectedCells = SelectedCells.Count;
        int selectedItems = SelectedItems.Count;

        if (selectedCells > 1)
        {
            if (selectedItems == 0 || selectedCells % selectedItems != 0)
            {
                DataGridCellInfo cellInfo = SelectedCells[0];
                SelectedCells.Clear();
                SelectedCells.Add(cellInfo);
                CurrentCell = SelectedCells[0];
            }
        }
    }

私はこれがエレガントな解決策ではないことを知っていますが、これまでのところこれは期待どおりに機能します、他の誰かがより良い解決策を持っているなら私は感謝します

于 2012-07-23T23:56:09.113 に答える