DataGrid以编程方式选择最后一个单元格 [英] DataGrid select last cell programmatically

查看:88
本文介绍了DataGrid以编程方式选择最后一个单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当下面的DataGrid第一次且只有第一次获得焦点时(即,在其他控件获得焦点之后),应将最后一行,第二列作为焦点并进行编辑。

When the DataGrid below gets the focus for the first time and only the first time (ie, after some other control has had the focus), the last row, 2nd column should be focused and in edit.

我为DataGrid.GotFocus添加了一个处理程序,但是它是复杂的代码,无法在上面得到结果。

I added a handler for the DataGrid.GotFocus, but it's complicated code and not getting the result above.

有人能得到一个优雅的,防弹的解决方案吗?

Anyone got an elegant, bullet proof solution?

我对代码做了微小的修改

I made tiny modifications to the code


  1. 发件人应该始终是我想要的网格,所以我只是使用它而不是依赖名称

  2. 当SelectionUnit为FullRow时,因为我的网格在更改之前它对CellOrRowHeader来说,您
    显然无法调用SelectedCells.Clear()

以下代码:

private void OnDataGridKeyboardGotFocus(object sender, KeyboardFocusChangedEventArgs e)
{
    var dg = sender as DataGrid;
    if (_hasHadInitialFocus) return;

    var rowIndex = dg.Items.Count - 2;
    if (rowIndex >= 0 && dg.Columns.Count - 1 >= 0)
    {
        var column = dg.Columns[dg.Columns.Count - 1];
        var item = dg.Items[rowIndex];
        var dataGridCellInfo = new DataGridCellInfo(item, column);

        if (dg.SelectionUnit != DataGridSelectionUnit.FullRow) {
            dg.SelectedCells.Clear();
            dg.SelectedCells.Add(dataGridCellInfo);
        }
        else {
            var row = dg.GetRow(rowIndex);
            row.IsSelected = true;
        }

        dg.CurrentCell = dataGridCellInfo;
        dg.BeginEdit();
    }

    _hasHadInitialFocus = true;
}



新问题



当焦点移到窗口中的另一个控件然后返回到网格时,我想重复选择。
我以为我可以在LostFocus事件中将_hasHadInitialFocus闩锁设置为false,但是下面的代码会触发单元格更改。
您知道我应该如何更好地捕捉失去焦点的事件吗?您是否同意这是关闭闩锁的地方?

New Question

I want to repeat the selection when the focus goes to another control in the window and then back to the grid. I thought I could turn that _hasHadInitialFocus latch to false in a LostFocus event, but the code below is firing on cell changes. Do you know how I should be trapping the lost focus event better, and do you agree that is the place to turn the latch off?

    private void DataGridLostFocus(object sender, RoutedEventArgs e) {
        _hasHadInitialFocus = false;
    }


推荐答案

您可能不得不摆弄偏移量取决于是否有新的项目行可见,但这对我有用。

You may have to fiddle with the offsets depending on whether there's an new item row visible or not, but this works for me.

    private bool _hasHadInitialFocus;

    private void DataGridGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
    {
        if (!_hasHadInitialFocus)
        {
            if (dataGrid.Items.Count - 2 >= 0 && dataGrid.Columns.Count - 1 >= 0)
            {
                var dataGridCellInfo = new DataGridCellInfo(
                    dataGrid.Items[dataGrid.Items.Count - 2], dataGrid.Columns[dataGrid.Columns.Count - 1]);

                dataGrid.SelectedCells.Clear();
                dataGrid.SelectedCells.Add(dataGridCellInfo);
                dataGrid.CurrentCell = dataGridCellInfo;
                dataGrid.BeginEdit();
            }

            _hasHadInitialFocus = true;
        }
    }

我注意到单击网格会留下一个选定的单元格目标单元格处于编辑模式。如果需要,可以使用以下解决方案:

I noticed that clicking into the grid leaves one cell selected and the target cell in edit mode. A solution to this if required is:

    private void DataGridGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
    {
        EditCell();
    }

    private void PreviewMouseLBDown(object sender, MouseButtonEventArgs e)
    {
        if (!_hasHadInitialFocus)
        {
            e.Handled = true;
            EditCell();
        }
    }

    private void EditCell()
    {
        if (!_hasHadInitialFocus)
        {
            if (dataGrid.Items.Count - 2 >= 0 && dataGrid.Columns.Count - 1 >= 0)
            {
                var dataGridCellInfo = new DataGridCellInfo(
                    dataGrid.Items[dataGrid.Items.Count - 2], dataGrid.Columns[dataGrid.Columns.Count - 1]);

                dataGrid.SelectedCells.Clear();
                dataGrid.SelectedCells.Add(dataGridCellInfo);
                dataGrid.CurrentCell = dataGridCellInfo;
                dataGrid.BeginEdit();
            }

            _hasHadInitialFocus = true;
        }
    }

这篇关于DataGrid以编程方式选择最后一个单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆