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 ...
随机推荐
- linux清理磁盘
https://blog.csdn.net/u012660464/article/details/78923011 有时候,服务突然挂了,再次启动却启动不了.一看,原来是磁盘空间被占满啦,那么,怎么清 ...
- 用VC实现特定编辑框上对回车键响应
一.引言 在通常的以CEditView为基类的单文档/多文档视图程序中,可以很好的响应键盘输入的回车键,只需比较最近两次的输入的字符,看看最新输入的字符是否内码是13(0x0d,回车键的内码)即可识别 ...
- osgi.net框架
osgi.net是一个动态的模块化框架.它向用户提供了模块化与插件化.面向服务构架和模块扩展支持等功能.该平台是OSGi联盟定义的服务平台规范移植到.NET的实现. 简介 尤埃开放服务平台是一个基于. ...
- ADO.NET操作SQL Server:数据库操作类(未封装)
1.添加数据 /// <summary> /// 添加数据 /// </summary> /// <param name="newEntity"> ...
- WPF Path 画箭头
代码: <Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.c ...
- @componentscan注解的用法和作用
刚刚开始学习spring boot,在application中忘记加上@componentscan,倒置web请求一直没有都是404,@componentscan的作用如下: @ComponentSc ...
- java学习笔记—HttpServletResponse(21)
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, ...
- 使用html2canvas截图并用jspdf打印的问题
之前的方案确实可以打印出a4的大小的pdf,但是也呈现了诸多问题,因为这种方法是截图然后再进行打印的,所以打印出来的效果是模糊的,思前想后决定放弃了这种方式. 最终还是决定使用浏览器自带的打印方法. ...
- spring包下载方法
http://blog.csdn.net/liangtiaoxian/article/details/52780747 https://jingyan.baidu.com/article/2fb0ba ...
- Python dict转化为string方法
dict-->string: str() string-->dict eval()(这个只是网上看的,没实测)