仅在ListBox中右键单击允许选择项目

时间:2013-12-21 14:14:51

标签: c# wpf listbox

我还在尝试这样做。不知道怎么样。我有一个ListBox with Drag& Drop来移动项目并添加新项目。我要做的是当用户用鼠标左键单击项目时没有任何反应。当他用鼠标右键单击它时,它会选择它(多选)。 我试过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
        {
            [...]
        }
    }

任何人都可以帮忙吗?鼠标左键不会选择任何项目。鼠标右键可以选择项目(多个)。

1 个答案:

答案 0 :(得分:0)

再看看你的代码。在你的问题中,你在询问e.LeftButton,只需将其改为e.RightButoon ......

相关问题