拖放 - 按钮事件在listviewitems中不起作用

时间:2015-07-29 06:56:43

标签: c# wpf listview events drag-and-drop

我已经用两个listview构建了一个自己的拖放。我列表视图中的项目有文本块和按钮。当我选择一个项目,当我点击我的listviewitem中的按钮时,它不起作用,因为他跳入我的移动功能进行拖放,但他必须进入点击事件,我不知道为什么。如果没有选择任何项目,它将起作用。

这是两个组成部分。 以下是我在XAML中的一些代码,我认为可以:

<ListView x:Name="LeftListView" Grid.Row="1" ItemsSource="{Binding}" ScrollViewer.PanningMode="None"
                  ItemContainerStyle="{StaticResource Itemstyle}"
                  AllowDrop="True" FontSize="18" FontWeight="Light" 
                  PreviewMouseLeftButtonDown="ListViewOnPreviewMouseLeftButtonDown" 
                  PreviewMouseLeftButtonUp="listView_PreviewMouseLeftButtonUp" 
                  PreviewMouseMove="ListViewOnMouseMove" Drop="ListViewOnDrop"
                  SelectionChanged="ListViewOnSelectionChanged" BorderThickness="1" BorderBrush="#EE7F00"></ListView>

<Button x:Name="CompleteProjectButton" Margin="0, 0, 3, 3" Style="{StaticResource ButtonTemplate}"    Click="CompleteProjectButton_Click 
                                            Background="Transparent" Grid.Column="2" Grid.Row="1" Grid.RowSpan="2" Visibility="Visible">
                                    <Border Background="Transparent" Height="25" Width="25">
                                        <Path Stretch="Uniform" Fill="#059600" 
                                              Data="F1 M 23.7501,33.25L 34.8334,44.3333L 52.2499,22.1668L 56.9999,26.9168L 34.8334,53.8333L 19.0001,38L 23.7501,33.25 Z ">
                                        </Path>
                                    </Border>
</Button>

这是背后的代码,每当选择一个项目时他都会跳过这个事件:

private ListView dragSource;
    private void ListViewOnMouseMove(object sender, MouseEventArgs mouseEventArgs)
    {
        dragSource = (sender as ListView);
        if (mouseEventArgs.LeftButton == MouseButtonState.Pressed)
        {
            if (dragSource.SelectedItems.Count > 0)
            {
                var items = (dragSource.SelectedItems as IList);
                dragSource.AllowDrop = true;
                DataObject dataObject = new DataObject(typeof(Collection), items);
                DragDrop.DoDragDrop((sender as ListView), dataObject, DragDropEffects.Copy);
            }
        }
    }

这就是简单的点击事件:

private void CompleteProjectButton_Click(object sender, RoutedEventArgs e)
    {
        MessageBox.Show(Translations.BatchCompleted);
    }

以及其他拖放方法:

private void ListViewOnPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        var clickedItem = getClickedItem(e);
        if (clickedItem == null)
            return;         
        if (((clickedItem as ListViewItem).IsSelected) && (!Keyboard.IsKeyDown(Key.LeftShift)) &&
            (!Keyboard.IsKeyDown(Key.RightShift)))
        {
            _mouseOnSelected = true;
            e.Handled = true;
        }
    }

 private ListViewItem getClickedItem(MouseButtonEventArgs e)
    {
        var dep = (DependencyObject)e.OriginalSource;
        while ((dep != null) && !(dep is ListViewItem))
        {
            dep = VisualTreeHelper.GetParent(dep);
        }
        var item = dep as ListViewItem;
        return item;
    }

    private void ListViewOnDrop(object sender, DragEventArgs dragEventArgs)
    {
        var items = dragEventArgs.Data.GetData(typeof(Collection)) as IList;
        if (items != null)
        {
            dragSource.AllowDrop = true;
            var listView = dragEventArgs.Source as ListView;
            var itemCache = new ArrayList(items);
            var itemAtPosition = VisualTreeHelper.HitTest(sender as ListView, dragEventArgs.GetPosition((sender as ListView)));
            var dataContext = itemAtPosition.VisualHit.GetValue(DataContextProperty);
            int itemIndex = -1;
            if (dataContext != null)
                itemIndex = (sender as ListView).Items.IndexOf(dataContext);
            listView.SelectedItem = null;
            foreach (var item in itemCache)
            {
                RemoveItemFromSourceView(item, dragSource);
                AddItemToDestinationView(item, listView, itemIndex);
                selectMovedItem(listView, item);
            }
            _selectedListView = listView;
        }
    }

这是一个非常奇怪的拖累,但我希望每个人都可以帮助我,谢谢。

0 个答案:

没有答案