WPF Popup focus in data grid

心不动则不痛 提交于 2020-01-10 02:02:22

问题


I'm creating a custom UserControl to be used inside a DataGrid editing template. It looks like this:

<UserControl
   x:Class="HR.Controls.UserPicker"
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:tk="http://schemas.microsoft.com/wpf/2008/toolkit"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Grid>
        <TextBlock x:Name="PART_TextBox" Text="Hello WOrld" />
        <Popup Width="234" Height="175" IsOpen="True" StaysOpen="True"

             Placement="Bottom"
             PlacementTarget="{Binding ElementName=PART_TextBox}"
         >
            <TextBox
                  x:Name="searchTextBox"
                  Text="&gt;Enter Name&lt;"/>
        </Popup>
    </Grid>
</UserControl>

edit: I've narrowed down the code a bit. It seems that if I put a Popup with textbox inside the CellEditingTemplate directly the textbox gets focus no problem. When I move that code into a UserControl I can no longer select the textbox when editing the cell.

Is the UserControl doing something funny with the focus ?


The problem is when i edit the cell in the datagrid I get the user control showing up but I can't click in the TextBox searchTextBox. When I click on it the popup closes and the cell goes back to default.

I have tried copying and pasting all the code inside the user control and pasting it directly into the CellEditingTemplate and that interacts the way it should.

I was just wondering if the UserControl did something weird that prevents a popup from gaining focus because it works as expected when directly placed in the CellEditingTemplate ?

Thanks, Raul


回答1:


I had a similar problem where a Popup embedded in a UserControl as a cell editing template would close when certain areas of it were clicked. The problem turned out to be that the WPF Toolkit (and presumably WPF4) DataGrid is very greedy with left mouse clicks. Even when you handle them and set Handled to true, the grid can interpret them as clicking into a different cell.

This thread has the full details, but the fix is to hook into DataGrid.CellEditEnding event and cancel the end edit:

private static void DataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
    if (e.Column.GetType() == typeof(DataGridTemplateColumn))
    {
        var popup = GetVisualChild<Popup>(e.EditingElement);
        if (popup != null && popup.IsOpen)
        {
            e.Cancel = true;
        }
    }   
}

private static T GetVisualChild<T>(DependencyObject visual)
    where T : DependencyObject
{
    if (visual == null)
        return null;

    var count = VisualTreeHelper.GetChildrenCount(visual);
    for (int i = 0; i < count; i++)
    {
        var child = VisualTreeHelper.GetChild(visual, i);

        var childOfTypeT = child as T ?? GetVisualChild<T>(child);
        if (childOfTypeT != null)
            return childOfTypeT;
    }

    return null;
}

Full credit for this goes to the Actipro thread.




回答2:


Not sure if this will help anyone, but this helps if you have custom controls in the datagrid with a popup..... this fixed my problem, and it was one line of xaml. I spend the whole day re-reading this forum and then looking at the source for DataGridCell. Hope this helps.

    <Style TargetType="{x:Type DataGridCell}">
        <Setter Property="Focusable" Value="False"></Setter>
    </Style>



回答3:


I had a kinda simular problem, i created a usercontrol containing a textbox, a button and a calendar. Basicaly i create my own datepicker with custom validation logic.

I put this component in a CellEditingTemplate. When i pressed the button, the popup showed, but clicking the popup anywhere caused the cell te stop editing (because the popup was taking focus from the textbox). I solved it by adding code that sais that if the popup is open, the focus of the textbox may not be lost. This did the trick for me.

Also, the in the on loaded event handler of the usercontrol i give focus to the textbox. In your case it's propably the Usercontrol itsefl that has focus.

protected override void OnPreviewLostKeyboardFocus(KeyboardFocusChangedEventArgs e) {
             // Don't allow focus to leave the textbox if the popup is open
             if (Popup.IsOpen) e.Handled = true;  
}

private void Root_Loaded(object sender, RoutedEventArgs e) {
       TextBox.Focus();
}



回答4:


Set FocusManager.IsFocusScope Attached Property on the Popup to True



来源:https://stackoverflow.com/questions/1784977/wpf-popup-focus-in-data-grid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!