简体   繁体   English

双击选择所有ListBoxItems

[英]Selecting all ListBoxItems on double-click

I have hooked into the ListBoxItems' double-click event using the ff. 我已经使用ff钩住了ListBoxItems的双击事件。 code in my XAML: 我的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. 当我运行它时,我看到了调试输出,但是并没有在屏幕上选择所有项目。

Try with SelectionMode as multiple. 尝试使用SelectionMode多个。

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. 在扩展模式下,对其执行双击的项目将重置为SelectedItem,这是因为在同一线程上执行了选择单个项目的click事件操作。

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. 为了做到这一点,我在双击事件处理程序上调用了(开始调用-这是异步的)委托方法(在类范围内),然后从那里调用主窗口Dispatcher上的ListBox的SelectAll调用。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM