手势功能在手持平台应用较为丰富,PC上的应用还不是很多,不过还是有一些软件已应用这个功能如遨游浏览器等,应用得当还是相当可以丰富用户的互交体验的。

接下来我将介绍如何为ListBox添加手势功能支持。

这里我们用到了InkCanvas,它有一个Gesture事件,在这个事件中我们可以得到我们所画出的形状的区域及

e.Strokes[0].GetGeometry(),然后我们对这ListBox的这个区域做命中检查VisualTreeHelper.HitTest,从中过滤出我们想选中的多个ListBoxItem.

效果图:

WPF使ListBox支持手势多选功能WPF使ListBox支持手势多选功能

代码如下:

m_ic_Gesture"> <ListBox Name="listBox1" SelectionMode="Extended" Height="350" Width="780"> <ListBoxItem Content="Ok" /> <ListBoxItem Content="Maxzhang" /> <ListBoxItem Content="houxiaodong" /> <ListBoxItem Content="hao1" /> <ListBoxItem Content="wuhao" /> <ListBoxItem Content="xiaoge" /> <ListBoxItem Content="wefwefwefwef" /> </ListBox> </InkCanvas> <Button Content="鼠标指针" Grid.Column="1" Height="23" HorizontalAlignment="Left" Margin="9,356,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="NoneMode_Click" /> <Button Content="手势圈选" Grid.Column="1" Height="23" HorizontalAlignment="Left" Margin="90,356,0,0" Name="button2" VerticalAlignment="Top" Width="101" Click="SelectionMode_Click" /> <Button Content="得到选中的项" Grid.Column="1" Height="23" HorizontalAlignment="Left" Margin="600,356,0,0" Name="button3" VerticalAlignment="Top" Width="97" Click="GetSelected_Click" />
 //鼠标指针模式
        private void NoneMode_Click(object sender, RoutedEventArgs e)
        {
            m_ic.EditingMode = InkCanvasEditingMode.None;
        }
        //手势选择模式
        private void SelectionMode_Click(object sender, RoutedEventArgs e)
        {
            m_ic.EditingMode = InkCanvasEditingMode.GestureOnly;
            listBox1.SelectedItem = null;
        }
        //得到选中的项
        private void GetSelected_Click(object sender, RoutedEventArgs e)
        {
           int count = listBox1.SelectedItems.Count;
           StringBuilder msg = new StringBuilder("Selected items:");
            for (int i = 0; i < count; i++)
            {
                var item = listBox1.SelectedItems[i] as ListBoxItem;
                msg.Append(item.Content);
                msg.Append(",");
            }
            msg.Append("Count:");
            msg.Append((count-1).ToString());
            MessageBox.Show(msg.ToString());
        }

        //被选中的项
        private List<ListBoxItem> _selections = new List<ListBoxItem>(5);   
        //InkCanvas的手势事件
        private void m_ic_Gesture(object sender, InkCanvasGestureEventArgs e)
        {
            listBox1.SelectedItem = null;    //清空选择
            _selections.Clear();             //
            //命中检测手势区域中ListBox1内的控件
            VisualTreeHelper.HitTest(listBox1, OnFilter, OnResult, new GeometryHitTestParameters(e.Strokes[0].GetGeometry()));
            foreach (ListBoxItem item in _selections)
            {
                item.IsSelected = !item.IsSelected;
            }
        }

        //过滤检测
        private HitTestFilterBehavior OnFilter(DependencyObject potentialHitTestTarget)
        {
            ListBoxItem item = potentialHitTestTarget as ListBoxItem;
            if (item != null)   //查找是ListBoxItem的控件
            {
                _selections.Add(item);
                return HitTestFilterBehavior.ContinueSkipSelfAndChildren;
            }
            return HitTestFilterBehavior.ContinueSkipSelf;
        }

        private HitTestResultBehavior OnResult(HitTestResult result)
        {
            return HitTestResultBehavior.Continue;
        }

 

相关文章:

  • 2022-12-27
  • 2022-02-17
  • 2021-05-27
  • 2022-12-23
  • 2022-12-28
  • 2021-11-18
  • 2021-09-25
  • 2021-08-27
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-05-19
  • 2022-12-23
  • 2021-07-11
  • 2021-08-27
  • 2022-12-23
相关资源
相似解决方案