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时,不能使用鼠标来滚动翻页的更多相关文章

  1. ScrollView中嵌套ListView时,listview高度显示的问题

    方法一:直接更改listview的控件高度,动态获取(根据条目和每个条目的高度获取) 前几天因为项目的需要,要在一个ListView中放入另一个ListView,也即在一个ListView的每个Lis ...

  2. 解决在eclipse中配置Tomcat时,出现"Cannot create a server using the selected type"的错误

    比如说使用tomcat 这是因为你之前创建过一次,比如说tomcat6,你指定的目录是:D:/tomcat-6.0.3 后来因为某种原因你把tomcat删了,然后你又安装到了E:/tomcat-6.0 ...

  3. 解决 java循环中使用 Map时 在put值时value值被覆盖的问题

    其实很简单,只需要把容器换成list 然后在循环中,每次循环末尾map = new HashMap() 或者直接在循环中一开始就实例化hashmap(Map map = new HashMap();) ...

  4. 解决WPF的ScrollViewer在使用触摸屏时,滑到尽头窗口抖动的情况

    原文:解决WPF的ScrollViewer在使用触摸屏时,滑到尽头窗口抖动的情况 wpf的ScrollViewer在触摸条件下 默认在尽头时会有一个窗口一起被拖动的FeedBack,但对用户的交互很不 ...

  5. [WPF]解决ListView在没有Items时,水平滚动条不出现的问题

    转载地址:http://www.cnblogs.com/nankezhishi/archive/2010/03/19/FixListViewNotScrollHeaderBug.html 在上一篇Bl ...

  6. 解决ScrollView中嵌套ListView滚动效果冲突问题

    在ScrollView中嵌套使用ListView,ListView只会显示一行到两行的数据.起初我以为是样式的问题,一直在对XML文件的样 式进行尝试性设置,但始终得不到想要的效果.后来在网上查了查, ...

  7. PullToRefreshListView中嵌套ViewPager滑动冲突的解决

    PullToRefreshListView中嵌套ViewPager滑动冲突的解决 最近恰好遇到PullToRefreshListView中需要嵌套ViewPager的情况,ViewPager 作为头部 ...

  8. 【Linux开发】OpenCV在ARM-linux上的移植过程遇到的问题4---共享库中嵌套库带路径【已解决】

    [Linux开发]OpenCV在ARM-linux上的移植过程遇到的问题4-共享库中嵌套库带路径[已解决] 标签:[Linux开发] 紧接着上一篇,我居然又尝试了一下编译opencv,主要是因为由于交 ...

  9. 在Linux下安装PHP过程中,编译时出现错误的解决办法

    在Linux下安装PHP过程中,编译时出现configure: error: libjpeg.(a|so) not found 错误的解决办法 configure: error: libjpeg.(a ...

随机推荐

  1. 原生JS实现随机点名项目

    核心思想 随机产生规定范围内的整数,然后再产生相同范围内的整数,两者相同时,则暂停. 所用知识 Math.random() * num: 产生从0到num的随机数 Math.floor(): 向下取整 ...

  2. Git Note - git tag

    git tag is used to create labels, usually for version numbers. Format: git tag <TagName> <r ...

  3. docker 版本变化及说明

    Docker从1.13.x版本开始,版本分为企业版EE和社区版CE,版本号也改为按照时间线来发布,比如17.03就是2017年3月,有点类似于ubuntu的版本发布方式. 企业版自然会提供一些额外的服 ...

  4. Javascript设计模式理论与实战:组合模式

    我们平时开发过程中,一定会遇到这种情况:同时处理简单对象和由简单对象组成的复杂对象,这些简单对象和复杂对象会组合成树形结构,在客户端对其处理的时候要保持一致性.比如电商网站中的产品订单,每一张产品订单 ...

  5. 在 Docker 中部署 ASP.NET CORE 应用

    有了 Docker 之后, 部署起来却这间非常方便,环境不用搭了, 直接创建一个 microsoft/aspnetcore 的容器, 在本地开发好后, 把内容直接部署到容器中. 下面的命令是把本地发布 ...

  6. 回去看linux的指令

    SYNC CL : MSM8953 @ CL#:12212299 PROJECT PATH : // Platform / N / NILE / COMBINATION / MSM8953 Cross ...

  7. php如何进行多进程与异步调用方法

    浏览器和服务器之间只一种面向无连接的HTTP协议进行通讯的,面向无连接的程序的特点是客户端请求服务端,服务端根据请求输出相应的程序,不能保持持久连接. 这样就出现了一个问题,一个客户端的相应服务端可能 ...

  8. “全栈2019”Java多线程第十五章:当后台线程遇到finally

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...

  9. Linux 中排除掉筛选的文件

    以下命令以网站目录www为例做介绍,有时候更新网站的时候需要保留比如图片目录,或者其他目录就需要这样的操作 实例一: 删除文件夹内所有文件只保留一个文件命令 [root@linuxzgf www]# ...

  10. 第三天,爬取伯乐在线文章代码,编写items.py,保存数据到本地json文件中

        一. 爬取http://blog.jobbole.com/all-posts/中的所有文章     1. 编写jobbole.py简单代码 import scrapy from scrapy. ...