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

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

问题描述

当下面的DataGrid第一次获得焦点时,只有第一次(即,在一些其他控件有焦点之后),最后一行第2列应该被重点和编辑。

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?

干杯,

Berryl

Cheers,
Berryl

我对代码进行了微小的修改

I made tiny modifications to the code


  1. 发件人应该始终是我想要的网格,所以我只是用而不是依靠一个名字

  2. 当SelectionUnit为FullRow时,由于我的网格之前我将其更改为CellOrRowHeader,您
    显然无法调用SelectedCells.Clear )

  1. the sender should always be the grid I want, so I just used that instead of relying on a name
  2. When the SelectionUnit is FullRow, as my grid was before I changed it to CellOrRowHeader you apparently can't call SelectedCells.Clear()

private void OnDataGridKeyboardGotFocus(object sender,KeyboardFocusChangedEventArgs e)
{

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;
    }



另一个新问题!



我发现DataGrid是一个PITA,有用的信息很短。最好的链接(Vin Sibal,Jamie Rodriquez)都很旧(当它是ToolKit的一部分)。

Another new question!

I find the DataGrid to be a PITA and short on useful info. The best links (Vin Sibal, Jamie Rodriquez) are all really old (when it was part of the ToolKit).

你知道任何更新的参考,真的很好?

Do you know of any updated reference that is really good?

推荐答案

您可能需要根据是否有新项目行可见或不可见,而是适用于我。

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天全站免登陆