Silverlight4Beta之鼠标滚轮支持增强

鼠标滚轮支持在sl3中就出现了,所有的UIElement都提供MouseWheel事件,然后我们可以通过MouseWheelEventArgs中的相关参数处理滚轮触发时的具体细节(Maxthon中滚轮一直不可用,据说FF也不可用)。

很强大,而且看上去这没什么问题。不过一到实际项目中我们就会发现这里有一些麻烦,或者说有一些繁琐。因为我们要处理的绝大部分(或者是全部)鼠标滚轮事件只是控制Control的滚动条而已。于是我们不得不一个一个的写MouseWheel事件,后来学乖了,搞个AttachedProperty上去。我们不仅一次的想说:为什么sl不能内置鼠标滚轮对于滚动条控制的默认支持呢?还好,微软在sl中的的又一次自我反省完成了这个小小的自我救赎。

一个很简单的例子:

.cs和.xaml分别如下

public MainPage() {
            InitializeComponent();
            this.Loaded += (s, e) => {
                this.DataContext = from i in Enumerable.Range(1, 200)
                                   select string.Format("第 {0} 条", i);
            };
        }
<UserControl x:Class="SilverlightApplication1.MainPage"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             d:DesignHeight="300"
             d:DesignWidth="400">

    <Grid x:Name="LayoutRoot"
          Background="White" MouseWheel="LayoutRoot_MouseWheel">
        <ListBox ItemsSource="{Binding}" />
    </Grid>

</UserControl>

F5运行后你会发现滚轮直接可以控制滚动条了,并且滚轮事件在Maxthon下同样好用,哦也!

image

作者:紫色永恒

出处:http://024hi.cnblogs.com/

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利

原文地址:https://www.cnblogs.com/024hi/p/1608172.html