Ver código fonte

页面编辑-补充鼠标框选逻辑

ZhouJieSheng 2 anos atrás
pai
commit
9a7744af27

+ 13 - 0
PDF Office/Views/PageEdit/PageEditContent.xaml

@@ -258,6 +258,9 @@
                 Background="#E7E9EC"
                 ItemTemplate="{StaticResource PageEditListBoxItemTemplate}"
                 ItemsSource="{Binding PageEditItems}"
+                PreviewMouseLeftButtonDown="ListPageEdit_PreviewMouseLeftButtonDown"
+                PreviewMouseLeftButtonUp="ListPageEdit_PreviewMouseLeftButtonUp"
+                PreviewMouseMove="ListPageEdit_PreviewMouseMove"
                 ScrollBar.Scroll="ListPageEdit_Scroll"
                 ScrollViewer.ScrollChanged="ListPageEdit_ScrollChanged"
                 SelectedIndex="{Binding ListSelectedIndex, Mode=TwoWay}"
@@ -285,6 +288,16 @@
                     </i:EventTrigger>
                 </i:Interaction.Triggers>
             </ListBox>
+            <!--  框选的矩形框  -->
+            <Rectangle
+                Name="RectChoose"
+                Grid.Row="1"
+                HorizontalAlignment="Left"
+                VerticalAlignment="Top"
+                Fill="#AACCEE"
+                Opacity="0.3"
+                Stroke="#AACCEE"
+                StrokeThickness="2" />
             <cus:ToastControl
                 Grid.Row="1"
                 Width="auto"

+ 159 - 0
PDF Office/Views/PageEdit/PageEditContent.xaml.cs

@@ -18,6 +18,7 @@ using System.Windows.Media.Imaging;
 using System.Windows.Navigation;
 using System.Windows.Shapes;
 using System.Windows.Threading;
+using WpfToolkit.Controls;
 
 namespace PDF_Office.Views.PageEdit
 {
@@ -47,6 +48,16 @@ namespace PDF_Office.Views.PageEdit
         /// </summary>
         private DispatcherTimer timer = new DispatcherTimer();
 
+        /// <summary>
+        /// 判断是否开始框选
+        /// </summary>
+        private bool startChoose = false;
+
+        /// <summary>
+        /// 框选的起始位置
+        /// </summary>
+        private Point starPosition = new Point();
+
         /// <summary>
         /// 记录当前滑块的状态
         /// </summary>
@@ -221,5 +232,153 @@ namespace PDF_Office.Views.PageEdit
                 (sender as Control).Focus();
             }
         }
+
+        private void ListPageEdit_PreviewMouseMove(object sender, MouseEventArgs e)
+        {
+            try
+            {
+                if (e.LeftButton == MouseButtonState.Pressed)
+                {
+                    if (startChoose)
+                    {
+                        var position = e.GetPosition(ListPageEdit);
+                        if (position.X < 5 || position.X > ListPageEdit.ActualWidth - 5 || position.Y < 5 || position.Y > ListPageEdit.ActualHeight - 5)
+                        {
+                            startChoose = false;
+                            RectChoose.Visibility = Visibility.Collapsed;
+                            Mouse.Capture(null);//暂时未想到靠近顶部和底部自动翻滚的好方法,只能先屏蔽这部分功能
+                            return;
+                        }
+                        DoSelectItems();
+                        return;
+                    }
+                    //拖拽排序的逻辑
+                    //var pos = e.GetPosition(ListPageEdit);
+                    //if (pos.Y < 0 || pos.Y > ListPageEdit.ActualHeight)
+                    //{
+                    //    MidLane.Visibility = Visibility.Collapsed;
+                    //    return;
+                    //}
+                    //HitTestResult result = VisualTreeHelper.HitTest(PageEditListBox, pos);
+                    //if (result == null)
+                    //{
+                    //    return;
+                    //}
+                    //var listBoxItem = CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
+                    //if (listBoxItem == null)
+                    //{
+                    //    return;
+                    //}
+                    //isDragingEnter = false;
+                    //tempItem = listBoxItem;
+                    //var panel = GetPanel(tempItem);
+
+                    //var item_pos = e.GetPosition(panel);
+                    //if (item_pos != null)
+                    //{
+                    //    item_x = item_pos.X;
+                    //    item_y = item_pos.Y;
+                    //}
+                    //var scroll = GetScrollHost(PageEditListBox);
+                    //DataObject dataObj = new DataObject(listBoxItem.Content as StackPanel);
+                    //DragDrop.DoDragDrop(PageEditListBox, dataObj, DragDropEffects.Move);
+                    //Mouse.Capture(PageEditListBox);
+                    //return;
+                }
+                RectChoose.Visibility = Visibility.Collapsed;
+                startChoose = false;
+                Mouse.Capture(null);
+            }
+            catch (Exception ex)
+            {
+               // MidLane.Visibility = Visibility.Collapsed;
+            }
+        }
+
+        private void ListPageEdit_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
+        {
+            var pos = e.GetPosition(ListPageEdit);
+            HitTestResult result = VisualTreeHelper.HitTest(ListPageEdit, pos);
+            if (result == null)
+            {
+                return;
+            }
+            //未选中item  并且不是点击滑轨时 开始框选
+            var listBoxItem = CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
+            var scroller = CommonHelper.FindVisualParent<ScrollBar>(result.VisualHit);
+            if (listBoxItem == null)
+            {
+                if (scroller != null)
+                {
+                    startChoose = false;
+                    return;
+                }
+                //点击空白处时开始框选
+                startChoose = true;
+                ListPageEdit.SelectedItems.Clear();
+                starPosition = e.GetPosition(ListPageEdit);
+                starPosition = new Point(starPosition.X, starPosition.Y + GetWrapPanel(ListPageEdit).VerticalOffset);
+                Mouse.Capture(ListPageEdit);
+                return;
+            }
+            startChoose = false;//选中了item 时,不能框选
+        }
+
+        private void ListPageEdit_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
+        {
+            startChoose = false;
+            Mouse.Capture(null);
+            RectChoose.Visibility = Visibility.Collapsed;
+        }
+
+        /// <summary>
+        /// 获取Listobox的Wrappanel容器
+        /// </summary>
+        /// <param name="listBox"></param>
+        /// <returns></returns>
+        public VirtualizingWrapPanel GetWrapPanel(ListBox listBox)
+        {
+            Border border = VisualTreeHelper.GetChild(listBox, 0) as Border;
+            var panel = CommonHelper.FindVisualChild<VirtualizingWrapPanel>(border);
+            return panel;
+        }
+
+        /// <summary>
+        ///根据鼠标拖选的框 选中里面的Item
+        /// </summary>
+        private void DoSelectItems()
+        {
+            var s = GetScrollHost(ListPageEdit);
+            Point start = new Point();
+            //通过 实时的垂直偏移量和第一次的偏移量抵消,来获取准确的垂直偏移值。
+            start = new Point(starPosition.X, starPosition.Y - s.VerticalOffset);
+            var rec = new Rect(start, Mouse.GetPosition(ListPageEdit));
+            RectChoose.Margin = new Thickness(rec.Left, rec.Top, 0, 0);
+            RectChoose.Width = rec.Width;
+            RectChoose.Height = rec.Height;
+            RectChoose.Visibility = Visibility.Visible;
+
+            //检测遍历所有项,筛选在矩形框中的Item
+            for (int i = 0; i < ListPageEdit.Items.Count; i++)
+            {
+                var _item = ListPageEdit.ItemContainerGenerator.ContainerFromIndex(i) as ListBoxItem;
+                //通过这一步来避免重复误选中
+                var parent = CommonHelper.FindVisualParent<VirtualizingWrapPanel>(_item);
+                if (parent == null)
+                    continue;
+
+                var v = VisualTreeHelper.GetOffset(_item);
+                if (rec.IntersectsWith(new Rect(v.X, v.Y, _item.ActualWidth, _item.ActualHeight)))
+                {
+                    ListPageEdit.SelectedItems.Add(ListPageEdit.Items[i]);
+                }
+                else
+                {
+                    ListPageEdit.SelectedItems.Remove(ListPageEdit.Items[i]);
+                }
+            }
+            return;
+        }
+
     }
 }