仅允许在 ListBox 中右键单击来选择项目
Posted
技术标签:
【中文标题】仅允许在 ListBox 中右键单击来选择项目【英文标题】:Allow select items only with right click in ListBox 【发布时间】:2014-01-10 06:36:05 【问题描述】:我仍在尝试这样做。不知道怎么做。我有一个带有拖放功能的 ListBox 来移动项目并添加新项目。我想要做的是当用户用鼠标左键单击项目时没有任何反应。当他用鼠标右键单击时,它会选择它(多选)。
我试过e.Handled = true
,问题是它不允许用户用鼠标滚动。我的列表框的事件:
private void LB_SongList_Drop(object sender, DragEventArgs e)
ListBox parent = sender as ListBox;
Song data = e.Data.GetData(typeof(Song)) as Song;
Song objectToPlaceBefore = GetObjectDataFromPoint(parent, e.GetPosition(parent)) as Song;
if (data != null && objectToPlaceBefore != null)
[...]//Code that moves object
parent.SelectedItems.Remove(data);
else
String[] file = e.Data.GetData(DataFormats.FileDrop, true) as String[];
if (file != null)
[...]//Code that adds new data
private void LB_SongList_PreviewMouseMove(object sender, MouseEventArgs e)
if (!b_IsScrolling)
if (e.LeftButton == MouseButtonState.Pressed)
MainPointer.Main_AllowClose = false;
ListBox parent = sender as ListBox;
Song data = GetObjectDataFromPoint(parent, e.GetPosition(parent)) as Song;
if (data != null)
parent.SelectedItems.Remove(data);
DragDrop.DoDragDrop(parent, data, DragDropEffects.Move);
private static object GetObjectDataFromPoint(ListBox source, Point point)
UIElement element = source.InputHitTest(point) as UIElement;
if (element != null)
object data = DependencyProperty.UnsetValue;
while (data == DependencyProperty.UnsetValue)
data = source.ItemContainerGenerator.ItemFromContainer(element);
if (data == DependencyProperty.UnsetValue) element = VisualTreeHelper.GetParent(element) as UIElement;
if (element == source) return null;
if (data != DependencyProperty.UnsetValue) return data;
return null;
private void LB_SongList_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
if (e.LeftButton == MouseButtonState.Pressed)
ListBox parent = sender as ListBox;
Song data = GetObjectDataFromPoint(parent, e.GetPosition(parent)) as Song;
if (data != null)
LB_SongList.SelectedItems.Remove(data);
[...]//Code that plays a song on double click (should allow only left mouse button)
private void LB_SongList_SelectionChanged(object sender, SelectionChangedEventArgs e)
if (LB_SongList.SelectedItems.Count != 0)
[...]
else
[...]
有人可以帮忙吗?鼠标左键不选择任何项目。鼠标右键确实选择项目(多个)。
【问题讨论】:
请将您的 xaml 发布给我们。 【参考方案1】:再看看你的代码。如果您在询问 e.LeftButton,只需将其更改为 e.RightButoon...
【讨论】:
以上是关于仅允许在 ListBox 中右键单击来选择项目的主要内容,如果未能解决你的问题,请参考以下文章