Why am I needing to click twice on a WPF listbox item in order to fire a command?

Posted by Donal on Stack Overflow See other posts from Stack Overflow or by Donal
Published on 2010-05-27T10:36:03Z Indexed on 2010/05/27 10:41 UTC
Read the original article Hit count: 176

Filed under:
|

Hi, I'm trying to make a standard WPF listbox be dynamically filled, and for each item in the list box to launch a command when clicked. Currently I have a working listbox, which can be filled and each item will fire the correct command, but in order to fire the command I have to click the list item twice. i.e, Click once to select the item, then click on the actual text to fire the command.

As the list is dynamically created, I had to create a data template for the list items:

<ListBox.ItemTemplate>
      <DataTemplate>
        <TextBlock Margin="4,2,4,2">
          <Hyperlink TextDecorations="None" Command="MyCommands:CommandsRegistry.OpenPanel">
            <TextBlock Text="{Binding}" Margin="4,2,4,2"/>
          </Hyperlink>
        </TextBlock>
      </DataTemplate>
 </ListBox.ItemTemplate>

Basically, how do I remove the need to click twice? I have tried to use event triggers to fire the click event on the hyperlink element when the list box item is selected, but I can't get it to work. Or, is there a better approach to dynamically fill a listbox and attach commands to each list item?

Thanks

© Stack Overflow or respective owner

Related posts about wpf

Related posts about listbox