当前位置: 移动技术网 > IT编程>开发语言>.net > WPF 可触摸移动的ScrollViewer控件

WPF 可触摸移动的ScrollViewer控件

2018年10月11日  | 移动技术网IT编程  | 我要评论

女师傅一体2,数据库关系模型,间客5200

listbox支持触摸滑动,而scrollviewer默认不支持。

scrollviewer如需要添加上下/左右触摸移动,需要在touch事件中处理。

处理如下:封装成一个用户控件

  1. touchdown事件中记录起始点,并添加对touchmove事件的监听
  2. touchup事件中注销touchmove事件的监听
  3. 在touchmove事件中,处理移动的偏移量。起始位置减去偏移量,即为当前滚动条的位置。

注:scrollviewer滚动到指定位置(指定位置=起始位置-移动的偏移量,滚动方向和手势方向相反)

 1     /// <summary>
 2     /// 可触摸滚动的scrollviewer控件
 3     /// </summary>
 4     public class touchablescrollviewer : scrollviewer
 5     {
 6         //触摸点的坐标
 7         point _startposition;
 8         //滚动条当前位置
 9         double _startverticaloffset;
10         double _starthorizontaloffset;
11         public touchablescrollviewer()
12         {
13             touchdown += touchablescrollviewer_touchdown;
14 
15             touchup += touchablescrollviewer_touchup;
16         }
17         private void touchablescrollviewer_touchdown(object sender, toucheventargs e)
18         {
19             //添加触摸移动监听
20             touchmove -= touchablescrollviewer_touchmove;
21             touchmove += touchablescrollviewer_touchmove;
22 
23             //获取scrollviewer滚动条当前位置
24             _startverticaloffset = verticaloffset;
25             _starthorizontaloffset = horizontaloffset;
26 
27             //获取相对于scrollviewer的触摸点位置
28             touchpoint point = e.gettouchpoint(this);
29             _startposition = point.position;
30         }
31 
32         private void touchablescrollviewer_touchup(object sender, toucheventargs e)
33         {
34             //注销触摸移动监听
35             touchmove -= touchablescrollviewer_touchmove;
36         }
37 
38         private void touchablescrollviewer_touchmove(object sender, toucheventargs e)
39         {
40             //获取相对于scrollviewer的触摸点位置
41             touchpoint endpoint = e.gettouchpoint(this);
42             //计算相对位置
43             double diffoffsety = endpoint.position.y - _startposition.y;
44             double diffoffsetx = endpoint.position.x - _startposition.x;
45 
46             //scrollviewer滚动到指定位置(指定位置=起始位置-移动的偏移量,滚动方向和手势方向相反)
47             scrolltoverticaloffset(_startverticaloffset - diffoffsety);
48             scrolltohorizontaloffset(_starthorizontaloffset - diffoffsetx);
49         }
50     }

 demo下载

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网