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. Python入门基础学习 一

    Python入门基础学习 一 Python下载及安装 下载地址:https://www.python.org/,选择最新的版本下载 稍等一会,安装完成. 简单语句 从idle启动Python:IDLE ...

  2. TFS签入代码时,自动修改工作项的状态为“已解决”

    Visual Studio中有一个很酷的功能,就是签入代码到TFS库时,可以关联相应的工作项,实现代码与工作项(需求.任务.Bug等)的关联,从而实现代码的跟踪. 在关联工作项的过程中,如果工作项具备 ...

  3. 搭建 .NET Core 开发环境

    安装 .Net Core 执行代码 任务时间:时间未知 .NET Core 的官方文档很详细,本实验带你建立一个.NET Core 1.1的Web运行环境,更多内容可以可以查阅微软官方文档. 安装 . ...

  4. StarUML3.0选择不同类型图和导出

    StarUML(简称SU),是一种创建UML类图,生成类图和其他类型的统一建模语言(UML)图表的工具. 可绘制9款UML图:用例图.类图.序列图.状态图.活动图.通信图.构件图.部署图以及复合结构图 ...

  5. [UWP开发]处理手机后退事件

    众所周知,uwp程序是一套代码,可以run在不同的平台上.但是不同的设备肯定有其独特之处,所以针对这些独特之处,必须用“独特的代码”来处理. 所以微软提供了一系列的拓展类库来实现这种特殊处理. 如上图 ...

  6. 那些令人敬佩的刚学OI的大佬

    我是萌新刚学OI,请问LCT怎么写常树最小啊 我是女生刚学OI,请问树链剖分哪里写挂了? 萌新求教,这棵SBT哪里有问题啊啊啊…… 刚学OI,请问可持久化非确定状态AC自动分块维护线段平衡仙人掌优化最 ...

  7. AtcoderExaWizards 2019题解

    传送门 \(A\ Regular\ Triangle\) 咕咕 \(B\ Red\ or\ Blue\) 咕咕咕 \(C\ Snuke\ the\ Wizard\) 我可能脑子真的坏掉了-- 容易发现 ...

  8. PHP中php_sapi_name()与array_map()

    1,php_sapi_name() php_sapi_name返回web服务器和php之间的接口类型.函数说明: string php_sapi_name(void) 返回描述php所使用的接口类型的 ...

  9. WebLogic “Java 反序列化”过程远程命令执行

    WebLogic “Java 反序列化”过程远程命令执行 详细信息: https://www.seebug.org/vuldb/ssvid-89726 说明: 反序列化是指特定语言中将传递的对象序列化 ...

  10. grunt 常用插件

    grunt-contrib-uglify:代码压缩 grunt-contrib-jshint:检查js拼写错误 csslint:检查css语法错误