zoukankan      html  css  js  c++  java
  • 自定义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);
            }
        }

    欢迎探讨WPF技术问题 QQ:281092346

  • 相关阅读:
    java框架---->mybatis的使用(一)
    java基础---->数组的基础使用(二)
    python爬虫---->github上python的项目
    java基础---->git的使用(一)
    spring基础---->请求与响应的参数(一)
    织梦DEDECMS网站后台安全检测提示 加一个开关
    MySql的join(连接)查询 (三表 left join 写法)
    html只允许输入的数据校验,只允许输入字母汉字数字等
    js控制只允许输入数字
    DEDECMS里面DEDE函数解析
  • 原文地址:https://www.cnblogs.com/tianhonghui/p/2153460.html
Copyright © 2011-2022 走看看