Fazer uma PerguntaFazer uma Pergunta
 

RespondidoListbox double click problems

  • quinta-feira, 2 de julho de 2009 21:12Chris128 Medalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     Contém Código
    OK so I have done the following to allow my listbox items (which use a datatemplate and are bound to an ObservableCollection in my code behind) to have a double click event:

    <ListBox.ItemContainerStyle>
                                <Style TargetType="ListBoxItem">
                                    <EventSetter Event="MouseDoubleClick" Handler="User_DoubleClick"/>
                                    <Setter Property="Template" Value="{DynamicResource ListBoxItemHighlightCT}"/>
                                </Style>
    </ListBox.ItemContainerStyle>
    
    So then my User_DoubleClick sub basically just does a few things and then needs to show a new window. The problem is that although it shows this new window, the window never has focus and so is hidden behind the main window that has the listbox on it. Im assuming the reason for this is because after the double click event is fired it runs the code in my User_DoubleClick sub and then returns focus to the main window.

    The only 'solution' I have at the moment is to just use a timer in the Loaded event of the new window so that after say 500 ms it just uses Me.Activate() to bring itself into focus but needless to say thats a pretty rubbish solution so has anyone else got any suggestions? I tried doing Window.Activate() after caling Window.Show() in my double click event but that doesnt help.

    PS if anyone has a better way of achieving the whole double click event on a datatemplated listbox item then im all ears

    Thanks
    Chris
    Windows XP MCP

Respostas

  • quinta-feira, 2 de julho de 2009 22:38Mark SalsberyMVPMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     RespondidoContém Código
    Nope because its not really an event handler, its just a sub that the event is routed to via the XAML I posted in my original post.

    This is how I declare it:
     Private Sub User_DoubleClick()
    
    
    Seen as it is used in an event setter in a style the way it is, how could I make it a 'proper' event handler?
    Windows XP MCP

    Declare it like this perhaps (C#)

    void User_DoubleClick(Object sender, MouseButtonEventArgs e)
    

    *edit*  In c# I can't successfully build unless I use the proper delegate for User_DoubleClick()
    • EditadoMark SalsberyMVPquinta-feira, 2 de julho de 2009 22:44
    • Marcado como RespostaChris128 segunda-feira, 6 de julho de 2009 19:37
    •  

Todas as Respostas

  • quinta-feira, 2 de julho de 2009 22:04Mark SalsberyMVPMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     
    Do you mark the event as handled in your event handler?  If not, does that help?


    Mark

    Mark Salsbery Microsoft MVP - Visual C++
  • quinta-feira, 2 de julho de 2009 22:13Chris128 Medalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     Contém Código
    Nope because its not really an event handler, its just a sub that the event is routed to via the XAML I posted in my original post.

    This is how I declare it:
     Private Sub User_DoubleClick()
    
    Seen as it is used in an event setter in a style the way it is, how could I make it a 'proper' event handler?
    Windows XP MCP
  • quinta-feira, 2 de julho de 2009 22:38Mark SalsberyMVPMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     RespondidoContém Código
    Nope because its not really an event handler, its just a sub that the event is routed to via the XAML I posted in my original post.

    This is how I declare it:
     Private Sub User_DoubleClick()
    
    
    Seen as it is used in an event setter in a style the way it is, how could I make it a 'proper' event handler?
    Windows XP MCP

    Declare it like this perhaps (C#)

    void User_DoubleClick(Object sender, MouseButtonEventArgs e)
    

    *edit*  In c# I can't successfully build unless I use the proper delegate for User_DoubleClick()
    • EditadoMark SalsberyMVPquinta-feira, 2 de julho de 2009 22:44
    • Marcado como RespostaChris128 segunda-feira, 6 de julho de 2009 19:37
    •  
  • segunda-feira, 6 de julho de 2009 19:37Chris128 Medalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     
    Thanks Mark that worked perfectly :)
    Windows XP MCP