Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could handle the <a href="http://msdn.microsoft.com/en-us/library/system.windows.uielement.mouseleftbuttondown%28v=vs.95%29.aspx" rel="nofollow"><code>MouseLeftButtonDown</code></a> and <a href="http://msdn.microsoft.com/en-us/library/system.windows.uielement.keydown%28v=vs.95%29.aspx" rel="nofollow"><code>KeyDown</code></a> events for the <code>ListBox</code>. For the <code>KeyDown</code> event, you'll need to check the <code>EventArgs</code> to determine whether the Enter key was pressed (as opposed to any other key). </p> <p>These events can fire even when an item is not selected (e.g., if the user clicks inside the <code>ListBox</code> but not over an actual item), so within your event handlers you should check for this. </p> <p>Your event handlers might look something like this:</p> <pre><code>public void MyListBox_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { ItemSelected(); } public void MyListBox_KeyDown(object sender, KeyEventArgs e) { if ((e.Key &amp; Key.Enter) == Key.Enter) { ItemSelected(); } } public void ItemSelected() { if (MyListBox.SelectedItem != null) { // Handle item selection here } } </code></pre> <p>These are off the top of my head, so you may need to tweak these slightly to get them to work exactly right. Hopefully you see the general idea though.</p> <hr> <p>Another way to do it would be to simply remove the <code>SelectionChanged</code> event handler when populating the <code>ListBox</code> with items (use the "<code>-=</code>" syntax), then re-attach it once this operation is complete. </p> <p>I'd recommend doing it this way (since you're concerned about the event firing when the list is populated). It wouldn't stop the users from selecting items using the Up and Down arrow keys, but unless you have a really good reason for doing so you're making things unnecessary inconvenient (users don't want to be arbitrarily restricted from doing things that ought to work).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload