WPF DataGridTemplateColumn。我错过了什么吗? [英] WPF DataGridTemplateColumn. Am I missing something?

查看:229
本文介绍了WPF DataGridTemplateColumn。我错过了什么吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

     <data:DataGridTemplateColumn Header="Name">
        <data:DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Name}">
            </DataTemplate>
        </data:DataGridTemplateColumn.CellTemplate> 
        <data:DataGridTemplateColumn.CellEditingTemplate>
            <DataTemplate>
                <TextBox Text="{Binding Name}">
            </DataTemplate>
        </data:DataGridTemplateColumn.CellEditingTemplate> 
    </data:DataGridTemplateColumn>              

这是Template列的明确示例,对吧?那可能是错的?
所以,这是事情 - 当用户通过触摸TAB键导航DataGrid时,需要按TAB两次(!)才能在TextBox中编辑文本。即使他刚刚开始打字,我是否可以在用户获取列的焦点之后使其可编辑?

It's clear example of Template column, right? What could be wrong with that? So, here is the thing - when a user navigates through DataGrid with hitting TAB-key, it needs to hit the TAB twice(!) to be able to edit text in TextBox. How could I make it editable as soon as the user gets the column focus, I mean even if he just starts typing?

好的。我找到一个方法 - 进入Grid.KeyUp()我把代码放在下面:

Ok. I found a way - into Grid.KeyUp() I put the code below:

 if (Grid.CurrentColumn.Header.ToString() == "UserName")
        {
            if (e.Key != Key.Escape) 
            {
                Grid.BeginEdit();

                // Simply send another TAB press
                if (Keyboard.FocusedElement is Microsoft.Windows.Controls.DataGridCell)
                {
                    var keyEvt = new KeyEventArgs(Keyboard.PrimaryDevice, Keyboard.PrimaryDevice.ActiveSource, 0, Key.Tab) { RoutedEvent = Keyboard.KeyDownEvent };
                    InputManager.Current.ProcessInput(keyEvt);
                }
            }
        } 


推荐答案

您的问题源于事实,每个单元格将其编辑器放在首先接收焦点的内容控件中,然后您必须再次选择编辑器。如果您在GenerateEditingElement方法中查看DataGridTemplateColumn的代码,它会调用一个LoadTemplateContent方法:

your issue stems from the fact that each cell puts its editor in a content control which first receives focus, then you have to tab once again to the editor. If you have a look at the code for DataGridTemplateColumn in the GenerateEditingElement method it calls a method LoadTemplateContent which does this:

private FrameworkElement LoadTemplateContent(bool isEditing, object dataItem, DataGridCell cell)
{
    DataTemplate template = ChooseCellTemplate(isEditing);
    DataTemplateSelector templateSelector = ChooseCellTemplateSelector(isEditing);
    if (template != null || templateSelector != null)
    {
        ContentPresenter contentPresenter = new ContentPresenter();
        BindingOperations.SetBinding(contentPresenter, ContentPresenter.ContentProperty, new Binding());
        contentPresenter.ContentTemplate = template;
        contentPresenter.ContentTemplateSelector = templateSelector;
        return contentPresenter;
    }

    return null;
}

了解如何创建一个新的内容演示者来放置模板。其他人以各种方式处理了这个问题,我得出了我自己的列类型来处理这些东西。 (所以我不会创建一个额外的元素或设置内容主持人没有收到焦点)在这个示例他们正在使用焦点管理器来处理相同的问题(我没有测试过这个代码)

see how it creates a new content presenter to put the template in. Other people have dealt with this problem in a variety of ways, I derive my own column type to deal with this stuff. (so i dont create an extra element or set the content presenter to not receive focus) In this example they are using focus manager to deal with the same issue (i havent tested this code)

<tk:DataGridTemplateColumn.CellEditingTemplate>
   <DataTemplate>
      <Grid FocusManager.FocusedElement="{Binding ElementName=txt1}">
         <TextBox Name="txt1" Text="{Binding XPath=@ISBN}" 
                  BorderThickness="0" GotFocus="TextBox_GotFocus"/>
      </Grid>
   </DataTemplate>
</tk:DataGridTemplateColumn.CellEditingTemplate>

如果您将用户控件作为编辑器,则可以使用焦点管理器或使用的模式OnLoaded事件的事件处理程序。

If you have a user control as your editor then you can use the pattern with the focus manager or use an event handler for the OnLoaded event.

这篇关于WPF DataGridTemplateColumn。我错过了什么吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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