开发者

Selecting all ListBoxItems on double-click

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 开发者_如何学Csender, 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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜