zoukankan      html  css  js  c++  java
  • [转载]自定义Behavior 实现Listbox自动滚动到选中项

    自定义Behavior 实现Listbox自动滚动到选中项

    blend为我们提供方便的behavior来扩展我们的控件,写好之后就可以在blend中方便的使用了。

    下面是自定义的behavior来实现Listbox自动滚动到选中项

    其中this.AssociatedObject为使用该行为的控件。

    其中

    OnAttached()和OnDetaching()为必须重写的内容,通常可以在OnAttched()里面添加事件处理程序,来达到拓展的目的。

    public class AutoScrollBehavior : Behavior<ListBox> 
        { 
            protected override void OnAttached() 
            { 
                base.OnAttached(); 
                this.AssociatedObject.SelectionChanged += new SelectionChangedEventHandler(AssociatedObject_SelectionChanged); 
            } 
            void AssociatedObject_SelectionChanged(object sender, SelectionChangedEventArgs e) 
            { 
                if (sender is ListBox) 
                { 
                    ListBox listbox = (sender as ListBox); 
                    if (listbox.SelectedItem != null) 
                    { 
                        listbox.Dispatcher.BeginInvoke((Action)delegate 
                        { 
                            listbox.UpdateLayout(); 
                            listbox.ScrollIntoView(listbox.SelectedItem); 
                        }); 
                    } 
                } 
            } 
            protected override void OnDetaching() 
            { 
                base.OnDetaching(); 
                this.AssociatedObject.SelectionChanged -= 
                    new SelectionChangedEventHandler(AssociatedObject_SelectionChanged); 
            } 
        }

     

  • 相关阅读:
    第18课 类型萃取(2)_获取返回值类型的traits
    第17课 类型萃取(1)_基本的type_traits
    【ASP.NET MVC系列】浅谈数据注解和验证
    【ASP.NET MVC系列】浅谈NuGet在VS中的运用
    【ASP.NET MVC系列】浅谈ASP.NET MVC 视图
    【ASP.NET MVC系列】浅谈ASP.NET MVC运行过程
    【Java系列】Eclipse与Tomcat
    【java系列】java开发环境搭建
    【设计模式篇】工厂模式
    【架构篇】OCP和依赖注入
  • 原文地址:https://www.cnblogs.com/fx2008/p/2751629.html
Copyright © 2011-2022 走看看