mvvm how to make a list view auto scroll to a new Item in a list view

后端 未结 5 768
南旧
南旧 2020-12-03 05:19

I am using the MVVM pattern, I have a view which creates a new ViewModel, after the user clicks save, this view is closed and a seperate view is opened which di

5条回答
  •  死守一世寂寞
    2020-12-03 06:10

    This solution is for a ListBox, but it could be modified for a ListView... This will scroll the selected item into view when you change the selected item from the ViewModel.

    Class:

    /// 
    /// ListBoxItem Behavior class
    /// 
    public static class ListBoxItemBehavior
    {
        #region IsBroughtIntoViewWhenSelected
    
        /// 
        /// Gets the IsBroughtIntoViewWhenSelected value
        /// 
        /// 
        /// 
        public static bool GetIsBroughtIntoViewWhenSelected(ListBoxItem listBoxItem)
        {
            return (bool)listBoxItem.GetValue(IsBroughtIntoViewWhenSelectedProperty);
        }
    
        /// 
        /// Sets the IsBroughtIntoViewWhenSelected value
        /// 
        /// 
        /// 
        public static void SetIsBroughtIntoViewWhenSelected(
          ListBoxItem listBoxItem, bool value)
        {
            listBoxItem.SetValue(IsBroughtIntoViewWhenSelectedProperty, value);
        }
    
        /// 
        /// Determins if the ListBoxItem is bought into view when enabled
        /// 
        public static readonly DependencyProperty IsBroughtIntoViewWhenSelectedProperty =
            DependencyProperty.RegisterAttached(
            "IsBroughtIntoViewWhenSelected",
            typeof(bool),
            typeof(ListBoxItemBehavior),
            new UIPropertyMetadata(false, OnIsBroughtIntoViewWhenSelectedChanged));
    
        /// 
        /// Action to take when item is brought into view
        /// 
        /// 
        /// 
        static void OnIsBroughtIntoViewWhenSelectedChanged(
          DependencyObject depObj, DependencyPropertyChangedEventArgs e)
        {
            ListBoxItem item = depObj as ListBoxItem;
            if (item == null)
                return;
    
            if (e.NewValue is bool == false)
                return;
    
            if ((bool)e.NewValue)
                item.Selected += OnListBoxItemSelected;
            else
                item.Selected -= OnListBoxItemSelected;
        }
    
        static void OnListBoxItemSelected(object sender, RoutedEventArgs e)
        {
            // Only react to the Selected event raised by the ListBoxItem 
            // whose IsSelected property was modified.  Ignore all ancestors 
            // who are merely reporting that a descendant's Selected fired. 
            if (!Object.ReferenceEquals(sender, e.OriginalSource))
                return;
    
            ListBoxItem item = e.OriginalSource as ListBoxItem;
            if (item != null)
                item.BringIntoView();
        }
    
        #endregion // IsBroughtIntoViewWhenSelected
    }
    

    Add the xmlns to your view:

    xmlns:util="clr-namespace:YourNamespaceHere.Classes"
    

    Add the style to the resources of the Window/UserControl:

    
        
    
    

    Implement the listbox:

    
    

提交回复
热议问题