WindowsPhone8中LongListSelector的扩展解决其不能绑定SelectdeItem的问题

2020-12-13 13:53

阅读:409

标签:style   blog   http   io   os   ar   使用   for   sp   

微软在Wp8中集成了LongListSelector, 但是该控件在ViewModel中不能实现的SelectdeItem双向绑定,因为其不是DependencyProperty没办法只能实现扩展!

 

1.实现LongListSelector的扩展ExtendedSelector

   

  public enum PositionOnAdd
    {
        Top,
        Default,
        NewItem
    }
    public class ExtendedSelector : LongListSelector
    {
        public static readonly DependencyProperty SelectedItemProperty =
            DependencyProperty.Register("SelectedItem", typeof(object), typeof(ExtendedSelector), new PropertyMetadata(default(object)));

        public static readonly DependencyProperty SelectionModeProperty =
            DependencyProperty.Register("SelectionMode", typeof(SelectionMode), typeof(ExtendedSelector), new PropertyMetadata(SelectionMode.Single));

        public static readonly DependencyProperty RepositionOnAddStyleProperty =
            DependencyProperty.Register("RepositionOnAddStyle", typeof(PositionOnAdd), typeof(ExtendedSelector), new PropertyMetadata(PositionOnAdd.Default));

        public PositionOnAdd RepositionOnAddStyle
        {
            get { return (PositionOnAdd)GetValue(RepositionOnAddStyleProperty); }
            set { SetValue(RepositionOnAddStyleProperty, value); }
        }

        public SelectionMode SelectionMode
        {
            get { return (SelectionMode)GetValue(SelectionModeProperty); }
            set { SetValue(SelectionModeProperty, value); }
        }

        public new object SelectedItem
        {
            get { return GetValue(SelectedItemProperty); }
            set { SetValue(SelectedItemProperty, value); }
        }

        public ExtendedSelector()
        {
            SelectionChanged += (sender, args) =>
            {
                if (SelectionMode == SelectionMode.Single)
                    SelectedItem = args.AddedItems[0];
                else if (SelectionMode == SelectionMode.Multiple)
                {
                    if (SelectedItem == null)
                    {
                        SelectedItem = new List();
                    }

                    foreach (var item in args.AddedItems)
                    {
                        ((List)SelectedItem).Add(item);
                    }

                    foreach (var removedItem in args.RemovedItems)
                    {
                        if (((List)SelectedItem).Contains(removedItem))
                        {
                            ((List)SelectedItem).Remove(removedItem);
                        }
                    }
                }
            };

            Loaded += (sender, args) =>
            {
                ((INotifyCollectionChanged)ItemsSource).CollectionChanged += (sender2, args2) =>
                {
                    if (ItemsSource.Count > 0 && args2.NewItems != null)
                    {
                        switch (RepositionOnAddStyle)
                        {
                            case PositionOnAdd.NewItem:
                                int index = ItemsSource.IndexOf(args2.NewItems[0]);

                                if (index >= 0)
                                    ScrollTo(ItemsSource[index]);
                                break;
                            case PositionOnAdd.Top:
                                ScrollTo(ItemsSource[0]);
                                break;
                        }
                    }
                };
            };
        }
    }

 2.在xmal中使用 扩展ExtendedSelector 

      a.定义数据模板

         

   

  b.定义LongListSelector的Style

       

 

  c.Page页面代码的实现

        

 

  

实现效果:

             soscw.com,搜素材

说明: RepositionOnAddStyle="Top" 有Top,Default,NewItem ,Top 为加载完数据后展示从头部开始 NewItem为加载完数据后展示从尾部开始

WindowsPhone8中LongListSelector的扩展解决其不能绑定SelectdeItem的问题

标签:style   blog   http   io   os   ar   使用   for   sp   

原文地址:http://www.cnblogs.com/fxiaoquan/p/4056822.html

上一篇:python3中map用法

下一篇:8.5 JavaScript的BOM


评论


亲,登录后才可以留言!