tags:

views:

37

answers:

1

I have hooked into the ListBoxItems' double-click event using the ff. code in my XAML:

    <Style TargetType="{x:Type ListBoxItem}">
        <EventSetter Event="MouseDoubleClick" Handler="onMouseDoubleClickOnListBoxItem" />
    </Style>

The code for the handler is:

    private void onMouseDoubleClickOnListBoxItem(object sender, MouseButtonEventArgs e)
    {
        Debug.Print("Going to select all.");
        listBox.SelectAll();
        Debug.Print("Selected all.");
    }

When I run it, I see the debug output but the items do not all get selected on screen.

+1  A: 

Try with SelectionMode as multiple.

Updated,

In Extended mode the item on which double click is performed it reset as SelectedItem, this is because on the same thread the click event action of selecting single item is performed.

In order to achieve this did was on the double click event handler I invoke (begin invoke - this is async) a delegate method (in the class scope) and from there invoke SelectAll call for the listbox on the main window Dispatcher.

Like,

// delegate
delegate void ChangeViewStateDelegate ();

// on double click event invoke the custom method
private void onMouseDoubleClickOnListBoxItem (object sender, MouseButtonEventArgs e) {
    ChangeViewStateDelegate handler = new ChangeViewStateDelegate (Update);
    handler.BeginInvoke (null, null);
}

// in the custom method invoke the selectall function on the main window (UI which created the listbox) thread
private void Update () {
    ChangeViewStateDelegate handler = new ChangeViewStateDelegate (UIUpdate);
    this.Dispatcher.BeginInvoke (handler, null);
}

// call listbox.SelectAll
private void UIUpdate () {
    lstBox.SelectAll ();
}
bjoshi
SelectionMode is already at Extended.
Chry Cheng
Yes but I think that in Extended mode the item on which double click is performed it reset as selecteditem. I am not sure about this don't have a setup to verify.
bjoshi
Sorry, you're right. Multiple as the selection mode produces better behavior. There's still one item not selected, however: the item that I double-clicked. Also, I need to use Extended selection mode. If you edit your answer with the solution to both, I can undo the down vote and accept your answer.
Chry Cheng
Updated the answer
bjoshi
Works! Answer accepted and voted up. Thanks!
Chry Cheng