WPF ListBox没有DoubleClick事件,至少没有我所知道的。是否有解决此问题的方法,可以让我双击某个项目以使事件处理程序对它执行某些操作?谢谢你的帮助。WPF ListBoxItem双击?
回答
您始终可以重写ListItem控件模板并处理模板内的双击事件,例如在包含ListBox常规内容的不可见边框中。
This article shows you how to use a ControlTemplate with a ListBoxItem。除此之外,只需将处理程序添加到控件模板的最外层元素即可。
如果您有Expression Blend,可以使用它来提取现有的控件模板供您修改,以便您不必执行尽可能多的工作来确保新列表框的行为与旧的行为相同。
找到了一个更好的解决方案 - 见下文。我现在正在离开公认的答案,只是因为我很欣赏Ben在这个问题上的帮助。 – 2010-04-04 17:24:06
原来有一个ListBox的MouseDoubleClick事件。我将这个事件添加到我的ListBox并让事件处理程序处理我的任务,将选定的项目复制到另一个ListBox。所以,现在每当我双击一个项目时,它都会被复制。
这真的很方便。我没有为我的列表使用数据模板(只是想显示一些文本),并且这节省了我不得不将我的文本包装在模板化控件中。 – 2012-05-09 13:09:17
应该注意的是,当ListBox.MouseDoubleClicked事件发生时,鼠标会触发在控件内的任何位置双击。 IE:这包括不在任何'ListBoxItem'上的双击。 – 2013-03-26 20:14:47
@ChrisKerekes你是对的。作为参考,我认为这可以通过使用if(listBox.SelectedIndex == -1)return;'来解决。 – Marlos 2013-08-20 17:23:52
如果您使用数据绑定,那么这个问题是很容易在你的代码来解决
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding ObjectName}"
MouseDown="TextBlock_MouseDown"/>
</DataTemplate>
</ListBox.ItemTemplate>
那么后面,检查双击如下
private void TextBlock_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ClickCount>=2)
{
....
}
}
如果你的双次点击项目在双击之前没有被选中,那么它在事件处理程序中不会显示出来。您在该处理程序中的逻辑需要考虑到这一点
注意这个解决方案,如果你点击ListBoxItem但在文本块之外(在填充/边距的情况下),事件不会触发。这可能会也可能不会是理想的。 – Clay 2014-02-25 17:18:59
^为了解决这个问题,只需给TextBlock一个'Background',就像'Transparent'一样,然后你可以在TextBlock边界内的任意位置双击。 – 2015-05-22 09:20:36
谢谢,这对我来说比ListBox的内置MouseDoubleClick事件更好的解决方案,因为它将双击限制为实际项目而不会扩展黑客行为。 – germanSharper 2016-09-05 13:36:51
我已经使用了David的上述答案(它以“事实证明存在ListBox的MouseDoubleClick事件”开头),以生成基于他的方法,但实施与附加行为。
我不是说你不应该有任何代码背后,因为我知道有些情况下,不应该为任何代价避免。但是,这是您如何将基于事件的解决方案转换为符合MVVM标准的解决方案的另一个示例,该解决方案通过Event来命令绑定转换。
这是我的附加行为的代码:在XAML
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
/// <summary>
/// Class implements a <seealso cref="Selector"/> double click
/// to command binding attached behaviour.
/// </summary>
public class DoubleClickSelectorItem
{
#region fields
public static readonly DependencyProperty DoubleClickItemCommandProperty =
DependencyProperty.RegisterAttached("DoubleClickItemCommand",
typeof(ICommand),
typeof(DoubleClickSelectorItem),
new PropertyMetadata(null,
DoubleClickSelectorItem.OnDoubleClickItemCommand));
#endregion fields
#region constructor
/// <summary>
/// Class constructor
/// </summary>
public DoubleClickSelectorItem()
{
}
#endregion constructor
#region properties
#endregion properties
#region methods
#region attached dependency property methods
public static ICommand GetDoubleClickItemCommand(DependencyObject obj)
{
return (ICommand)obj.GetValue(DoubleClickItemCommandProperty);
}
public static void SetDoubleClickItemCommand(DependencyObject obj, ICommand value)
{
obj.SetValue(DoubleClickItemCommandProperty, value);
}
#endregion attached dependency property methods
private static void OnDoubleClickItemCommand(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var uiElement = d as Selector;
// Remove the handler if it exist to avoid memory leaks
if (uiElement != null)
uiElement.MouseDoubleClick -= UIElement_MouseDoubleClick;
var command = e.NewValue as ICommand;
if (command != null)
{
// the property is attached so we attach the Drop event handler
uiElement.MouseDoubleClick += UIElement_MouseDoubleClick;
}
}
private static void UIElement_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
var uiElement = sender as Selector;
// Sanity check just in case this was somehow send by something else
if (uiElement == null)
return;
// Is there a selected item that was double clicked?
if (uiElement.SelectedIndex == -1)
return;
ICommand doubleclickCommand = DoubleClickSelectorItem.GetDoubleClickItemCommand(uiElement);
// There may not be a command bound to this after all
if (doubleclickCommand == null)
return;
// Check whether this attached behaviour is bound to a RoutedCommand
if (doubleclickCommand is RoutedCommand)
{
// Execute the routed command
(doubleclickCommand as RoutedCommand).Execute(uiElement.SelectedItem, uiElement);
}
else
{
// Execute the Command as bound delegate
doubleclickCommand.Execute(uiElement.SelectedItem);
}
}
#endregion methods
}
用法:
<ListBox ItemsSource="{Binding CurrentItems}"
behav:DoubleClickSelectorItem.DoubleClickItemCommand="{Binding Path=NavigateDownCommand}"
/>
这是可能的,而不使用代码隐藏或附加的行为与参数ListBoxItem
s到结合命令,简单地通过使用InputBindings
与MouseBinding
,如之前在this answer中所示。
例ListBox
与MouseBinding
为LeftDoubleClick
:
<ListBox ItemsSource="{Binding MyDataSource}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding MySourceItemName}">
<TextBlock.InputBindings>
<MouseBinding MouseAction="LeftDoubleClick"
Command="{Binding DataContext.MyCommand, RelativeSource={RelativeSource AncestorType={x:Type ListBox}}}"
CommandParameter="{Binding MySourceItemId}" />
</TextBlock.InputBindings>
</TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
如果该命令被在相同的DataContext作为ListBox
的ItemsSource
定义,它可以通过使用RelativeSource
结合作为包括在实施例的限制。
那是最好的解决方案。我尝试过之前没有工作的EventToCommand。 InputBindings完美工作。我在ListBox数据模板的StackPanel上使用它们。 – BlueM 2016-03-23 16:29:51
好的有意义的解决方案。 – AnjumSKhan 2017-03-28 10:37:07
这是迄今为止最明智,最健壮的解决方案。 – 2017-12-29 01:23:30
- 1. WPF ListView双击
- 2. WPF ListBoxItem中选择
- 3. WPF双双击TreeViewItem子节点
- 4. WPF文本框和双击
- 5. WPF ListView GridViewColumn双击打印
- 6. ListBoxItem上的WPF弹出框
- 7. WPF ListBoxItem的文本换
- 8. WPF ListBox的ListBoxItem的绑定
- 9. WPF ListBoxItem MouseOver边框更改
- 10. WPF ListBoxItem的选择问题
- 11. WPF双击更改行背景颜色
- 12. 双击WPF Datagrid崩溃程序
- 13. TreeView,Overiding双击鼠标事件WPF
- 14. 双击WPF数据网格与RowDetailsTemplate
- 15. 在WPF DataGrid中双击显示RowDetails
- 16. WPF的ListView ScrollViewer中双击事件
- 17. CustomUserControl双击WPF中ControlTemplate中的事件
- 18. 更新没有ListBox.Items.Refresh()的WPF ListBoxItem?
- 19. 如何利用ListBoxItem内的空间WPF
- 20. 在WPF中向上/向下移动ListBoxItem
- 21. 如何获取WPF ListBoxItem属性的值
- 22. WPF中的ListBoxItem ControlTemplate设计问题?
- 23. ListBoxItem子项(WPF)中的鼠标交互
- 24. WPF ListBoxItem中和文本菜单
- 25. WPF Listbox高亮部分ListBoxItem元素
- 26. 如何正确绑定WPF中的ListBoxItem?
- 27. WPF:风格ListBoxItem统一颜色
- 28. WPF如何获得一个ListBoxItem的
- 29. 如何在ListBoxItem中将WPF CheckBox居中
- 30. 双击/双击Angular2&ionic
看看这个问题:http://stackoverflow.com/questions/821564/double-click-a-list-item – Eclipse 2010-03-30 18:13:53