Wpf DataGrid 的焦点问题

发布于 2024-12-10 17:54:37 字数 1720 浏览 3 评论 0原文

我正在使用 WPFToolKit DataGrid 来显示数据;在我的用例中,

  1. 用户单击一个单元格并输入一个值(我捕获该事件并将焦点设置到 TextBox
  2. 按 Enter
  3. 键值已提交
  4. 焦点转移到下一个单元格

现在,用户无法输入TextBox 中的任何值(在 DataGridCell 中),除非他单击该单元格。 TextBox 可以是各种控件的一部分(例如 NumericUpDownCalendar 等)。

此行为类似于 Excel,但我无法将焦点转移到底层 TextBox,因为 DataGridCell 中存在各种其他包装用户控件(类似于 DataGridCell 包含 MatrixCellContainer,其中包含 MatrixCell,其中包含 UpDown 控件)

任何指针都会非常有帮助。

更新:

我可以通过像这样处理 DataGridCell_Selected 事件来实现我正在寻找的内容:

private void DataGridCell_Selected(object sender, RoutedEventArgs e)
{
        Microsoft.Windows.Controls.DataGridCell dataGridCell = 
               sender as Microsoft.Windows.Controls.DataGridCell;

    // ToDo: This is a very bad hack; 
    // should be replaced by some proper technique
    if (dataGridCell != null)
    {
        NumericUpDownBase<int>[] IntUpDownControls = 
            dataGridCell.GetChildrenOfType<NumericUpDownBase<int>>();
        if (IntUpDownControls.Count() != 0)
        {
            IntUpDownControls[0].Focus();
            //Keyboard.Focus(IntUpDownControls[0]);
        }
    else
    {
        NumericUpDownBase<double>[] DblUpDownControls = 
                dataGridCell.GetChildrenOfType<NumericUpDownBase<Double>>();
         if (DblUpDownControls.Count() != 0)
         {
                 DblUpDownControls[0].Focus();
                 //Keyboard.Focus(DblUpDownControls[0]);
          }
    }
    }
 }

但我知道会有更好的方法来实现这一点!

I am using WPFToolKit DataGrid to display data; In my use case

  1. user clicks on a cell and enters a value (I trap the event and set the focus to the TextBox)
  2. Presses Enter
  3. Value is committed
  4. Focus shifts to Next Cell

Now, user can't enter any value in the TextBox(in the DataGridCell) unless he clicks on the cell. The TextBox can be a part of various controls(like NumericUpDown, Calendar etc.).

This behavior is similar to Excel but I am not able to shift the focus to the Underlying TextBox as various other wrapper user controls are there in the DataGridCell(something like DataGridCell contains MatrixCellContainer, which contains MatrixCell, which contains UpDown Control)

Any pointer will be really helpful.

Update:

I can achieve what I am looking for by handling DataGridCell_Selected event like this :

private void DataGridCell_Selected(object sender, RoutedEventArgs e)
{
        Microsoft.Windows.Controls.DataGridCell dataGridCell = 
               sender as Microsoft.Windows.Controls.DataGridCell;

    // ToDo: This is a very bad hack; 
    // should be replaced by some proper technique
    if (dataGridCell != null)
    {
        NumericUpDownBase<int>[] IntUpDownControls = 
            dataGridCell.GetChildrenOfType<NumericUpDownBase<int>>();
        if (IntUpDownControls.Count() != 0)
        {
            IntUpDownControls[0].Focus();
            //Keyboard.Focus(IntUpDownControls[0]);
        }
    else
    {
        NumericUpDownBase<double>[] DblUpDownControls = 
                dataGridCell.GetChildrenOfType<NumericUpDownBase<Double>>();
         if (DblUpDownControls.Count() != 0)
         {
                 DblUpDownControls[0].Focus();
                 //Keyboard.Focus(DblUpDownControls[0]);
          }
    }
    }
 }

But I know there will be a better way to achieve this!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

诠释孤独 2024-12-17 17:54:37

您如何将焦点设置到下一个单元格?

WPF 有两个版本的焦点:逻辑焦点和键盘焦点。我怀疑您正在使用 myDataGridCell.Focus(),它仅设置逻辑焦点。

myDataGridCell.Focus();         // Sets Logical Focus
Keyboard.Focus(myDataGridCell); // Sets Keyboard Focus

How are you setting Focus to the next cell?

WPF has two versions of focus, Logical Focus and Keyboard Focus. I suspect you are using myDataGridCell.Focus(), which only sets Logical Focus.

myDataGridCell.Focus();         // Sets Logical Focus
Keyboard.Focus(myDataGridCell); // Sets Keyboard Focus
飘过的浮云 2024-12-17 17:54:37

最后我解决了这个问题 -

private void HandleCellSelected(object sender, RoutedEventArgs e)
{
    DataGridCell dataGridCell = sender as DataGridCell;
    if (dataGridCell != null)
    {
        TextBox[] textboxcontrols = dataGridCell.GetChildrenOfType<TextBox>();
        if (textboxcontrols.Count() != 0)
        {
            textboxcontrols[0].Focus();
        }
    }
}

但仍在寻找更好的方法......

Finally I settled with this -

private void HandleCellSelected(object sender, RoutedEventArgs e)
{
    DataGridCell dataGridCell = sender as DataGridCell;
    if (dataGridCell != null)
    {
        TextBox[] textboxcontrols = dataGridCell.GetChildrenOfType<TextBox>();
        if (textboxcontrols.Count() != 0)
        {
            textboxcontrols[0].Focus();
        }
    }
}

Still looking for a better approach though...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文