[英]Selecting all ListBoxItems on double-click
我已经使用ff钩住了ListBoxItems的双击事件。 我的XAML中的代码:
<Style TargetType="{x:Type ListBoxItem}">
<EventSetter Event="MouseDoubleClick" Handler="onMouseDoubleClickOnListBoxItem" />
</Style>
处理程序的代码为:
private void onMouseDoubleClickOnListBoxItem(object sender, MouseButtonEventArgs e)
{
Debug.Print("Going to select all.");
listBox.SelectAll();
Debug.Print("Selected all.");
}
当我运行它时,我看到了调试输出,但是并没有在屏幕上选择所有项目。
尝试使用SelectionMode多个。
更新,
在扩展模式下,对其执行双击的项目将重置为SelectedItem,这是因为在同一线程上执行了选择单个项目的click事件操作。
为了做到这一点,我在双击事件处理程序上调用了(开始调用-这是异步的)委托方法(在类范围内),然后从那里调用主窗口Dispatcher上的ListBox的SelectAll调用。
喜欢,
// 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.