简体   繁体   English

只允许右键单击列表框选择项目

[英]Allow select items only with right click in ListBox

I'm still trying to do this. 我仍在尝试这样做。 No clue how. 不知道如何。 I have a ListBox with Drag&Drop to move items and add new. 我有一个具有拖放功能的列表框,用于移动项目并添加新项目。 What I'm trying to do is when user clicks on item with left mouse button nothing happens. 我想做的是当用户用鼠标左键单击项目时什么也没发生。 And when he clicks with right mouse button it selects it (Multiple selection). 当他用鼠标右键单击时,将其选中(多项选择)。 I have tried e.Handled = true , the problem is that it doesn't allow user to scroll with mouse. 我已经尝试过e.Handled = true ,问题是它不允许用户用鼠标滚动。 My listbox's events: 我的列表框的事件:

    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
        {
            [...]
        }
    }

Anyone could help with this? 任何人都可以帮忙吗? Left Mouse button doesn't select any item. 鼠标左键不选择任何项目。 Right Mouse Button does select items (Multiple). 鼠标右键确实会选择项目(多个)。

Take a look at you code again. 再次查看您的代码。 In your If's your are asking about e.LeftButton, just change it to e.RightButoon... 如果您正在询问e.LeftButton,只需将其更改为e.RightButoon ...

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

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