views:

450

answers:

1

Hello, 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"&gt;

    <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

A: 

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();
}
nick