WPF解决当ScrollViewer中嵌套ItemsControl时,不能使用鼠标来滚动翻页
1. ScrollViewer:滚动条容器,当内容超过指定的长度和宽度后,就会出现滚动条,而且可以使用鼠标中键来滚动,
简单例子如下:
<Window x:Class="ConnectScrollViewScrollingDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="" Width="">
<Grid>
<!--the normal control-->
<ScrollViewer>
<StackPanel>
<TextBlock Text=""></TextBlock>
<TextBlock Text=""></TextBlock>
<TextBlock Text=""></TextBlock>
<Label Content=""/>
<Label Content=""/>
<Label Content=""/>
<Label Content=""/>
<UserControl Content=""/>
<UserControl Content=""/>
<UserControl Content=""/>
<UserControl Content=""/>
<UserControl Content=""/>
<Button Content=""/>
<Button Content=""/>
<Button Content=""/>
<Button Content=""/>
<Button Content=""/>
<Rectangle Height="" Fill="Black"/>
<Rectangle Height="" Fill="Black"/>
<Rectangle Height="" Fill="Black"/>
<Rectangle Height="" Fill="Black"/>
<Rectangle Height="" Fill="Black"/>
<Rectangle Height="" Fill="Black"/>
</StackPanel>
</ScrollViewer>
</Grid>
</Window>
当在ScrollViewer中的控件是普通控件时,使用的情况一切正常
2.当ScrollViewer中包含ItemsControl时,因为ItemsControl自身也有滚动的功能,所以在未做任何处理下的例子:
<Window x:Class="ConnectScrollViewScrollingDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="" Width="">
<Grid> <!--with ListBox-->
<ScrollViewer Grid.Column="">
<StackPanel>
<ListBox Background="AliceBlue">
<ListBox.ItemsSource>
<x:Array Type="TextBox">
<TextBox Text=""/>
<TextBox Text=""/>
<TextBox Text=""/>
<TextBox Text=""/>
<TextBox Text=""/>
<TextBox Text=""/>
<TextBox Text=""/>
<TextBox Text=""/>
<TextBox Text=""/>
<TextBox Text=""/>
<TextBox Text=""/>
<TextBox Text=""/>
<TextBox Text=""/>
<TextBox Text=""/>
<TextBox Text=""/>
<TextBox Text=""/>
<TextBox Text=""/>
<TextBox Text=""/>
<TextBox Text=""/>
<TextBox Text=""/>
<TextBox Text=""/>
</x:Array>
</ListBox.ItemsSource>
</ListBox>
<ListBox Height="" Background="Green">
<ListBox.ItemsSource>
<x:Array Type="Rectangle">
<Rectangle Height="" Fill="Red"/>
<Rectangle Height="" Fill="Red"/>
<Rectangle Height="" Fill="Orange"/>
<Rectangle Height="" Fill="Red"/>
<Rectangle Height="" Fill="Gray"/>
<Rectangle Height="" Fill="Red"/>
<Rectangle Height="" Fill="Red"/>
</x:Array>
</ListBox.ItemsSource>
</ListBox>
</StackPanel>
</ScrollViewer>
</Grid>
</Window>
细心一点,你就会发现,其实仅仅是在ListBox的元素区域中使用鼠标滚轮无效,但在两个ListBox的交界处还是有效果的,所以
我们需要做的,就是讲ListBox的滚动事件与外层的ScrollViewer的滚动关联起来,核心代码如下:
public void UseTheScrollViewerScrolling(FrameworkElement fElement)
{
fElement.PreviewMouseWheel += (sender, e) =>
{
var eventArg = new MouseWheelEventArgs(e.MouseDevice, e.Timestamp, e.Delta);
eventArg.RoutedEvent = UIElement.MouseWheelEvent;
eventArg.Source = sender;
fElement.RaiseEvent(eventArg);
};
}
代码中使用了FrameworkElement类型的参数,因为这个方法的作用是让内层的ListBox与外层的ScrollViewer关联起来,但当两者
之间还有别的元素时,这个方法的参数应该传入的是ScrollViewer的直接子元素,而不是深层的ListBox。
完整代码:MainWindow.xaml
<Window x:Class="ConnectScrollViewScrollingDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="400" Width="900">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<!--the normal control-->
<ScrollViewer>
<StackPanel>
<TextBlock Text="123"></TextBlock>
<TextBlock Text="123"></TextBlock>
<TextBlock Text="123"></TextBlock>
<Label Content="456"/>
<Label Content="456"/>
<Label Content="456"/>
<Label Content="456"/>
<UserControl Content="789"/>
<UserControl Content="789"/>
<UserControl Content="789"/>
<UserControl Content="789"/>
<UserControl Content="789"/>
<Button Content="010"/>
<Button Content="010"/>
<Button Content="010"/>
<Button Content="010"/>
<Button Content="010"/>
<Rectangle Height="20" Fill="Black"/>
<Rectangle Height="20" Fill="Black"/>
<Rectangle Height="20" Fill="Black"/>
<Rectangle Height="20" Fill="Black"/>
<Rectangle Height="20" Fill="Black"/>
<Rectangle Height="20" Fill="Black"/>
</StackPanel>
</ScrollViewer> <!--with ListBox-->
<ScrollViewer Grid.Column="1">
<StackPanel>
<ListBox Background="AliceBlue">
<ListBox.ItemsSource>
<x:Array Type="TextBox">
<TextBox Text="00000"/>
<TextBox Text="00000"/>
<TextBox Text="00000"/>
<TextBox Text="00000"/>
<TextBox Text="00000"/>
<TextBox Text="00000"/>
<TextBox Text="00000"/>
<TextBox Text="00000"/>
<TextBox Text="00000"/>
<TextBox Text="00000"/>
<TextBox Text="00000"/>
<TextBox Text="00000"/>
<TextBox Text="00000"/>
<TextBox Text="00000"/>
<TextBox Text="00000"/>
<TextBox Text="00000"/>
<TextBox Text="00000"/>
<TextBox Text="00000"/>
<TextBox Text="00000"/>
<TextBox Text="00000"/>
<TextBox Text="00000"/>
</x:Array>
</ListBox.ItemsSource>
</ListBox>
<ListBox Height="50" Background="Green">
<ListBox.ItemsSource>
<x:Array Type="Rectangle">
<Rectangle Height="20" Fill="Red"/>
<Rectangle Height="20" Fill="Red"/>
<Rectangle Height="20" Fill="Orange"/>
<Rectangle Height="20" Fill="Red"/>
<Rectangle Height="20" Fill="Gray"/>
<Rectangle Height="20" Fill="Red"/>
<Rectangle Height="20" Fill="Red"/>
</x:Array>
</ListBox.ItemsSource>
</ListBox>
</StackPanel>
</ScrollViewer> <!--with ListBox and Grid-->
<ScrollViewer Grid.Column="2">
<Grid x:Name="grid">
<StackPanel>
<ListBox Background="AliceBlue">
<ListBox.ItemsSource>
<x:Array Type="TextBox">
<TextBox Text="00000"/>
<TextBox Text="00000"/>
<TextBox Text="00000"/>
<TextBox Text="00000"/>
<TextBox Text="00000"/>
<TextBox Text="00000"/>
<TextBox Text="00000"/>
<TextBox Text="00000"/>
<TextBox Text="00000"/>
<TextBox Text="00000"/>
<TextBox Text="00000"/>
<TextBox Text="00000"/>
<TextBox Text="00000"/>
<TextBox Text="00000"/>
<TextBox Text="00000"/>
<TextBox Text="00000"/>
<TextBox Text="00000"/>
<TextBox Text="00000"/>
<TextBox Text="00000"/>
<TextBox Text="00000"/>
<TextBox Text="00000"/>
</x:Array>
</ListBox.ItemsSource>
</ListBox>
<ListBox Height="50" Background="Green">
<ListBox.ItemsSource>
<x:Array Type="Rectangle">
<Rectangle Height="20" Fill="Red"/>
<Rectangle Height="20" Fill="Red"/>
<Rectangle Height="20" Fill="Orange"/>
<Rectangle Height="20" Fill="Red"/>
<Rectangle Height="20" Fill="Gray"/>
<Rectangle Height="20" Fill="Red"/>
<Rectangle Height="20" Fill="Red"/>
</x:Array>
</ListBox.ItemsSource>
</ListBox>
</StackPanel>
</Grid> </ScrollViewer>
</Grid>
</Window>
MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes; namespace ConnectScrollViewScrollingDemo
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
UseTheScrollViewerScrolling(grid);
} public void UseTheScrollViewerScrolling(FrameworkElement fElement)
{
fElement.PreviewMouseWheel += (sender, e) =>
{
var eventArg = new MouseWheelEventArgs(e.MouseDevice, e.Timestamp, e.Delta);
eventArg.RoutedEvent = UIElement.MouseWheelEvent;
eventArg.Source = sender;
fElement.RaiseEvent(eventArg);
};
}
}
}
WPF解决当ScrollViewer中嵌套ItemsControl时,不能使用鼠标来滚动翻页的更多相关文章
- ScrollView中嵌套ListView时,listview高度显示的问题
方法一:直接更改listview的控件高度,动态获取(根据条目和每个条目的高度获取) 前几天因为项目的需要,要在一个ListView中放入另一个ListView,也即在一个ListView的每个Lis ...
- 解决在eclipse中配置Tomcat时,出现"Cannot create a server using the selected type"的错误
比如说使用tomcat 这是因为你之前创建过一次,比如说tomcat6,你指定的目录是:D:/tomcat-6.0.3 后来因为某种原因你把tomcat删了,然后你又安装到了E:/tomcat-6.0 ...
- 解决 java循环中使用 Map时 在put值时value值被覆盖的问题
其实很简单,只需要把容器换成list 然后在循环中,每次循环末尾map = new HashMap() 或者直接在循环中一开始就实例化hashmap(Map map = new HashMap();) ...
- 解决WPF的ScrollViewer在使用触摸屏时,滑到尽头窗口抖动的情况
原文:解决WPF的ScrollViewer在使用触摸屏时,滑到尽头窗口抖动的情况 wpf的ScrollViewer在触摸条件下 默认在尽头时会有一个窗口一起被拖动的FeedBack,但对用户的交互很不 ...
- [WPF]解决ListView在没有Items时,水平滚动条不出现的问题
转载地址:http://www.cnblogs.com/nankezhishi/archive/2010/03/19/FixListViewNotScrollHeaderBug.html 在上一篇Bl ...
- 解决ScrollView中嵌套ListView滚动效果冲突问题
在ScrollView中嵌套使用ListView,ListView只会显示一行到两行的数据.起初我以为是样式的问题,一直在对XML文件的样 式进行尝试性设置,但始终得不到想要的效果.后来在网上查了查, ...
- PullToRefreshListView中嵌套ViewPager滑动冲突的解决
PullToRefreshListView中嵌套ViewPager滑动冲突的解决 最近恰好遇到PullToRefreshListView中需要嵌套ViewPager的情况,ViewPager 作为头部 ...
- 【Linux开发】OpenCV在ARM-linux上的移植过程遇到的问题4---共享库中嵌套库带路径【已解决】
[Linux开发]OpenCV在ARM-linux上的移植过程遇到的问题4-共享库中嵌套库带路径[已解决] 标签:[Linux开发] 紧接着上一篇,我居然又尝试了一下编译opencv,主要是因为由于交 ...
- 在Linux下安装PHP过程中,编译时出现错误的解决办法
在Linux下安装PHP过程中,编译时出现configure: error: libjpeg.(a|so) not found 错误的解决办法 configure: error: libjpeg.(a ...
随机推荐
- Flink本地环境安装部署
本次主要介绍flink1.5.1版本的本地环境安装部署,该版本要求jdk版本1.8以上. 下载flink安装包:http://archive.apache.org/dist/flink/flink-1 ...
- Linux 用户和文件
Linux系统中用户的扩展研究 进程 用户和文件 Linux中的用户及用户组 linux中只有两个等级:root和非root, 一个用户至少属于一个用户组 一个用户可以属于多个用户组 用户本身的区别主 ...
- .NET中Debug模式与Release模式差别
Debug里的PDB是full,保存着调试和项目状态信息.有断言.堆栈检查等代码.Release 里的PDB是pdb-only,基本上:出什么错了+错误在哪行. 因为很多人把PDB理解成:调试文件.P ...
- [Erlang24]使用zotonic搭建网站记录
zotonic的搭建网站(blog)记录: zotonic:用Erlang做的一个web 框架: 和wordpress 类似,但是官网称比PHP CMS要快10倍以上 先看看我的成果:正弦 ...
- Cesium开发实践汇总
一.简介.开发环境搭建 二.Viewer控件 三.地图图层介绍 四.地形介绍 五.坐标变换 六.CZML 七.3D模型
- @JoinColumn 详解
1. 一对一 现假设有Person表和Address表,是一对一的关系,在Person中有一个指向Address表主键的字段addressID,所以主控方一定是Person,所谓主控方就是能改变关联关 ...
- 「BZOJ4318」OSU!
题目链接 戳我 \(Solution\) 我们考虑每增加一个\(1\)会对答案有什么影响: \[E((x+1)^3)-E(x^3)=E(3x^2+3x+1)=3E(x^2)+3E(x)+1\] 所以我 ...
- Windows7 64位下SDK Manager.exe无法运行问题解决方法
我在Windows7 64位下运行SDK Manager.exe总是一闪而过,无法正常启动它,最后在网上找到一篇文章,修改系统“path”变量,把“path”变量中的第一项设置为我的JDK目录“C:\ ...
- .Net开发工程师笔试试题
第一部分[数据库技能] 附上自己做的答案,提出不足之处 现在有一个SQL Server 2000版本的数据库,里面包含有三个表Info.InfoReply.User,分别表示信息.信息评论和用户表,包 ...
- python --爬虫--爬取百度翻译
import requestsimport json class baidufanyi: def __init__(self, trans_str): self.lang_detect_url = ' ...