如何在绑定的silverlight列表框中自动选择第一项?

时间:2010-01-10 00:52:31

标签: silverlight data-binding listbox

使用silverlight,我有一个ListSource绑定到ObservableCollection的列表框,它是异步更新的。我想在绑定完成更新后自动选择列表框中的第一个项目。

我无法找到实现这一目标的好方法。我没有看到任何有用的事件要处理列表框,如果我绑定到集合的CollectionChanged事件,绑定还没有更新,所以如果我在那时设置listbox.selectedindex我得到一个例外,该值是出来的范围。有任何想法吗?也许某种方式挂钩绑定更新?

3 个答案:

答案 0 :(得分:1)

我花了很长时间在网上搜索这个问题的解决方案,基本上最终绊倒了解决方案。

您要做的是将列表框绑定到ICollectionView。然后确保没有将IsSynchronizedWithCurrentItem设置为false。

糟糕,无效

IsSynchronizedWithCurrentItem="False"

这是Silverlight的默认设置,不要浪费时间输入

IsSynchronizedWithCurrentItem="{x:Null}"

这会在运行时抛出错误,我相信与{x:null}相同

IsSynchronizedWithCurrentItem="True"

ICollectionView有一个名为 MoveCurrentToFirst 的方法。这个名字看起来有点模棱两可,但它确实将CurrentItem指针移动到第一个项目(我最初认为它通过将您选择的任何项目移动到第一个项目位置来重新排序集合)。 IsSynchronizedWithCurrentItem属性允许Listbox(或任何实现Selector的控件)与ICollectionViews一起工作。在您的代码中,您可以调用ICollectioView.CurrentItem而不是绑定Listbox.SelectedItem来获取当前选定的项目。

以下是我如何将ICollectionView用于我的视图(我正在使用MVVM):

public System.ComponentModel.ICollectionView NonModifierPricesView
        {
            get
            {
                if (_NonModifierPricesView == null)
                {
                    _NonModifierPricesView = AutoRefreshCollectionViewSourceFactory.Create(x => ((MenuItemPrice)x).PriceType == MenuItemPrice.PriceTypes.NonModifier);
                    _NonModifierPricesView.Source = Prices;
                    _NonModifierPricesView.ApplyFilter(x => ((MenuItemPrice)x).DTO.Active == true);
                }
                ICollectionView v = _NonModifierPricesView.View;
                v.MoveCurrentToFirst();
                return v;
            }
        }

现在,由于您要绑定到可观察集合,因此您无法使用默认的CollectionViewSource,因为它不知道源集合的更新。您可能已经注意到我正在使用名为 AutoRefreshCollectionViewSource 的自定义CVS实现。如果内存服务,我发现代码在线并修改它以供我自己使用。我已经添加了额外的过滤功能,因此可能会更多地清理这些类。

这是我的代码版本:

<强> AutoRefreshCollectionViewSource.cs

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Windows.Data;

    public class AutoRefreshCollectionViewSource : System.Windows.Data.CollectionViewSource
    {

        // A delegate for launching a refresh of the view at a different priority.
        private delegate void NoArgDelegate();
        private Predicate<object> MyFilter; // this is the filter we put on when we do ApplyFilter
        private Predicate<object> BaseFilter; // this is the filter that is applied always

        public AutoRefreshCollectionViewSource(Predicate<object> _baseFilter) : base() 
        {
            BaseFilter = _baseFilter;

            if (BaseFilter == null)
                BaseFilter = x => true;

        }

        /// <summary>
        /// A collection containing all objects whose event handlers have been
        /// subscribed to.
        /// </summary>
        private List<INotifyPropertyChanged> colSubscribedItems = new List<INotifyPropertyChanged>();

        // We must override the OnSourceChanged event so that we can subscribe
        // to the objects in the new collection (and unsubscribe from the old items).

        protected override void OnSourceChanged(object oldSource, object newSource)
        {
            // Unsubscribe from the old source.
            if (oldSource != null)
                SubscribeSourceEvents(oldSource, true);

            // Subscribe to the new source.
            if (newSource != null)
                SubscribeSourceEvents(newSource, false);

            base.OnSourceChanged(oldSource, newSource);
        }

        /// <summary>
        /// Adds or Removes EventHandlers to each item in the source collection as well as the
        /// collection itself (if supported).
        /// </summary>
        /// <param name="source">The collection to (un)subscribe to and whose objects should be (un)subscribed.</param>
        /// <param name="remove">Whether or not to subscribe or unsubscribe.</param>
        private void SubscribeSourceEvents(object source, bool remove)
        {
            // Make sure the source is not nothing.
            // This may occur when setting up or tearing down this object.
            if (source != null)

                if (source is INotifyCollectionChanged)
                    // We are (un)subscribing to a specialized collection, it supports the INotifyCollectionChanged event.
                    // (Un)subscribe to the event.
                    if (remove)
                        ((INotifyCollectionChanged)source).CollectionChanged -= Handle_INotifyCollectionChanged;
                    else
                        ((INotifyCollectionChanged)source).CollectionChanged += Handle_INotifyCollectionChanged;

            if (remove)
                // We are unsubscribing so unsubscribe from each object in the collection.
                UnsubscribeAllItemEvents();
            else
                // We are subscribing so subscribe to each object in the collection.
                SubscribeItemsEvents((IEnumerable)source, false);

        }

        /// <summary>
        /// Unsubscribes the NotifyPropertyChanged events from all objects
        /// that have been subscribed to. 
        /// </summary>
        private void UnsubscribeAllItemEvents()
        {
            while (colSubscribedItems.Count > 0)
                SubscribeItemEvents(colSubscribedItems[0], true);
        }

        /// <summary>
        /// Subscribes or unsubscribes to the NotifyPropertyChanged event of all items
        /// in the supplied IEnumerable.
        /// </summary>
        /// <param name="items">The IEnumerable containing the items to (un)subscribe to/from.</param>
        /// <param name="remove">Whether or not to subscribe or unsubscribe.</param>
        private void SubscribeItemsEvents(IEnumerable items, bool remove)
        {
            foreach (object item in items)
                SubscribeItemEvents(item, remove);
        }

        /// <summary>
        /// Subscribes or unsubscribes to the NotifyPropertyChanged event if the supplied
        /// object supports it.
        /// </summary>
        /// <param name="item">The object to (un)subscribe to/from.</param>
        /// <param name="remove">Whether or not to subscribe or unsubscribe.</param>
        private void SubscribeItemEvents(object item, bool remove)
        {
            if (item is INotifyPropertyChanged)
                // We only subscribe of the object supports INotifyPropertyChanged.

                if (remove)
                {
                    // Unsubscribe.
                    ((INotifyPropertyChanged)item).PropertyChanged -= Item_PropertyChanged;
                    colSubscribedItems.Remove((INotifyPropertyChanged)item);
                }
                else
                {
                    // Subscribe.
                    ((INotifyPropertyChanged)item).PropertyChanged += Item_PropertyChanged;
                    colSubscribedItems.Add((INotifyPropertyChanged)item);
                }
        }

        /// <summary>
        /// Handles a property changed event from an item that supports INotifyPropertyChanged.
        /// </summary>
        /// <param name="sender">The object that raised the event.</param>
        /// <param name="e">The event arguments associated with the event.</param>
        /// <remarks></remarks>
        private void Item_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            // By default, we do not need to refresh.
            bool refresh = false;

            if (e.PropertyName == "Active" || e.PropertyName == "DTO.Active")
                refresh = true;

            if (refresh)
                // Call the refresh.
                // Notice that the dispatcher will make the call to Refresh the view.  If the dispatcher is not used,
                // there is a possibility for a StackOverFlow to result.
                this.Dispatcher.BeginInvoke(new NoArgDelegate(this.View.Refresh), null);
        }

        /// <summary>
        /// Handles the INotifyCollectionChanged event if the subscribed source supports it.
        /// </summary>
        private void Handle_INotifyCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            switch (e.Action)
            {
                case NotifyCollectionChangedAction.Add:
                    SubscribeItemsEvents(e.NewItems, false);
                    break;
                case NotifyCollectionChangedAction.Remove:
                    SubscribeItemsEvents(e.OldItems, true);
                    break;
                case NotifyCollectionChangedAction.Replace:
                    SubscribeItemsEvents(e.OldItems, true);
                    SubscribeItemsEvents(e.NewItems, false);
                    break;
                case NotifyCollectionChangedAction.Reset:
                    UnsubscribeAllItemEvents();
                    SubscribeItemsEvents((IEnumerable)sender, false);
                    break;
            }
        }

        public void ApplyFilter(Predicate<object> f)
        {
            if (f != null)
                MyFilter = f;

            this.View.Filter = x => MyFilter(x) && BaseFilter(x);
            this.View.Refresh();
        }

        public void RemoveFilter()
        {
            this.View.Filter = BaseFilter;
            this.View.Refresh();
}
}

<强> AutoRefreshCollectionViewSourceFactory.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
    public class AutoRefreshCollectionViewSourceFactory
    {
        private static List<AutoRefreshCollectionViewSource> Collections;
        public static AutoRefreshCollectionViewSource Create()
        {
            if (Collections == null)
                Collections = new List<AutoRefreshCollectionViewSource>();
            AutoRefreshCollectionViewSource cvs = new AutoRefreshCollectionViewSource(null);
            Collections.Add(cvs);
            return cvs;
        }
        public static AutoRefreshCollectionViewSource Create(Predicate<object> p)
        {
            if (Collections == null)
                Collections = new List<AutoRefreshCollectionViewSource>();
            AutoRefreshCollectionViewSource cvs = new AutoRefreshCollectionViewSource(p);
            Collections.Add(cvs);
            return cvs;
        }
        public static void ApplyFilterOnCollections()
        {
            foreach (AutoRefreshCollectionViewSource cvs in Collections)
                cvs.ApplyFilter(null);
        }
        public static void RemoveFilterFromCollections()
        {
            foreach (AutoRefreshCollectionViewSource cvs in Collections)
                cvs.RemoveFilter();
        }
        public static void CleanUp()
        {
            Collections = null;
        }
}

答案 1 :(得分:1)

您可以做的另一件事是创建一个继承自ListBox的UserControl,通过覆盖OnItemsChanged方法公开ItemsChanged,并处理此事件并将ListBox的SelectedIndex设置为0。

public partial class MyListBox : ListBox
{
    public delegate void ItemsSourceChangedHandler(object sender, EventArgs e);

    #region Override
    protected override void OnItemsChanged(
        NotifyCollectionChangedEventArgs e)
    {
        base.OnItemsChanged(e);
        OnItemsChangedEvent(e);
    }
    #endregion Override

    #region Class Events

    public delegate void ItemsChangedEventHandler(object sender,
        NotifyCollectionChangedEventArgs e);
    public event ItemsChangedEventHandler ItemsChanged;

    private void OnItemsChangedEvent(
        NotifyCollectionChangedEventArgs e)
    {
        if (ItemsChanged != null)
        {
            ItemsChanged(this, e);
        }
    }

    #endregion Class Events

}

用户控件的XAML:

<ListBox x:Class="CoverArtRefiner.MyListBox"
    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">

    </Grid>
</ListBox>

在屏幕上添加以下内容:

xmlns:local="clr-namespace:<The Namespace which MyListBox is contained in>"

现在将自定义控件添加到您的窗口/ usercontrol:

<local:MyListBox ItemsChanged="listBox_ItemsChanged" Background="Black" />

最后处理这个事件:

private void listBox_ItemsChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
    MyListBox listBox = (MyListBox)sender;
    if (listBox.Items.Count > 0)
        listBox.SelectedIndex = 0;
}

答案 2 :(得分:0)

在ListBox上,将SelectedItem属性绑定到codebind或viewmodel上的属性。然后,在异步回调处理程序中将属性设置为集合中的第一个项目,并为属性引发PropertyChanged事件(除非您已在属性的setter中引发事件):

MySelectedListItem = _entitylist.FirstOrDefault();
RasisePropertyChanged("MySelectedListItem");