WPF DataGridTemplateColumn. Am I missing something?

前端 未结 4 412
死守一世寂寞
死守一世寂寞 2021-01-12 20:19
     
        
            
                &l         


        
4条回答
  •  终归单人心
    2021-01-12 20:38

    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)

    
       
          
             
          
       
    
    

    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.

提交回复
热议问题