码迷,mamicode.com
首页 > 其他好文 > 详细

WPF获取控件内部的ScrollViewer,并控制ScrollViewer操作

时间:2014-05-17 13:09:33      阅读:275      评论:0      收藏:0      [点我收藏+]

标签:style   class   c   tar   color   int   

//获取内部  ScrollViewer方法

public static T FindVisualChild<T>(DependencyObject obj) where T : DependencyObject
        {
            if (obj != null)
            {
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
                {
                    DependencyObject child = VisualTreeHelper.GetChild(obj, i);
                    if (child != null && child is T)
                    {
                        return (T)child;
                    }
                    T childItem = FindVisualChild<T>(child);
                    if (childItem != null) return childItem;
                }
            }
            return null;
        }

 

 

 

 

 

  //通过鼠标滑轮事件控制ScrollViewer滚动

  private void list_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            ItemsControl items = (ItemsControl)sender;
            ScrollViewer scroll = FindVisualChild<ScrollViewer>(items);
            if (scroll != null)
            {
                int d = e.Delta;
                if (d > 0)
                {
                    scroll.LineRight();
                }
                if (d < 0)
                {
                    scroll.LineLeft();
                }
                scroll.ScrollToTop();
            }
        }

WPF获取控件内部的ScrollViewer,并控制ScrollViewer操作,布布扣,bubuko.com

WPF获取控件内部的ScrollViewer,并控制ScrollViewer操作

标签:style   class   c   tar   color   int   

原文地址:http://www.cnblogs.com/chuhaida/p/3724776.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!