C# 双击选择所有ListBoxItems
我已经使用ff连接到ListBoxItems的双击事件。我的XAML中的代码:C# 双击选择所有ListBoxItems,c#,wpf,events,C#,Wpf,Events,我已经使用ff连接到ListBoxItems的双击事件。我的XAML中的代码: <Style TargetType="{x:Type ListBoxItem}"> <EventSetter Event="MouseDoubleClick" Handler="onMouseDoubleClickOnListBoxItem" /> </Style> 当我运行它时,我会看到调试输出,但屏幕上并没有选择所有项目。尝试将Selectio
<Style TargetType="{x:Type ListBoxItem}">
<EventSetter Event="MouseDoubleClick" Handler="onMouseDoubleClickOnListBoxItem" />
</Style>
当我运行它时,我会看到调试输出,但屏幕上并没有选择所有项目。尝试将SelectionMode设置为multiple 更新 在扩展模式下,执行双击的项目将重置为SelectedItem,这是因为在同一线程上执行了选择单个项目的单击事件操作 为了实现这一点,我在双击事件处理程序上调用(begininvoke-这是异步的)一个委托方法(在类作用域中),然后在主窗口调度程序上调用listbox的SelectAll调用 像
是的,但我认为在扩展模式下,双击的项目会重置为selecteditem。我对此不确定。我没有要验证的设置。对不起,你是对的。选择模式会产生更好的行为。但是,仍有一个项目未选中:我双击的项目。此外,我需要使用扩展选择模式。如果你用这两种方法编辑你的答案,我可以撤消否决票并接受你的答案。
private void onMouseDoubleClickOnListBoxItem(object sender, MouseButtonEventArgs e)
{
Debug.Print("Going to select all.");
listBox.SelectAll();
Debug.Print("Selected all.");
}
// 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 ();
}