Scroll to row when programmatically setting selected item

9 posts, 0 answers
  1. Maria
    Maria avatar
    14 posts
    Member since:
    Dec 2008

    Posted 08 Dec 2010 Link to this post

    Hello,

    I'm trying to implement a "find" feature where the user can enter a value in a textbox (outside of the grid) then click a button to find the row that matches the search criteria.  I'm able to programmatically set the selectitem on the grid successfully, but if the item is not in the current scroll region the user has to scroll down to to the item.  Is there a way to ensure that the selected item is always in view?  Also, I should mention that we are using MVVM, so there is no code behind.

    Thanks.
  2. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 08 Dec 2010 Link to this post

    Hi,

     You can use our ScrollIntoView() method to achieve your goal. Since your using MVVM you can create your custom attached behavior to do this - you can check for the project in this blog post for more info about attached behaviors. 

    Kind regards,
    Vlad
    the Telerik team
    Browse the videos here>> to help you get started with RadControls for WPF
  3. UI for WPF is Visual Studio 2017 Ready
  4. Thomas
    Thomas avatar
    28 posts
    Member since:
    Jun 2010

    Posted 20 Jul 2011 Link to this post

    The blog did not help me. Can you please give a example for this problem?

  5. Erik
    Erik avatar
    36 posts
    Member since:
    Jun 2012

    Posted 18 Aug 2012 Link to this post

    This is what I came up with

    In your ViewModel:
    private Requirement _scrollIntoViewObject;
    public Requirement ScrollIntoViewObject
    {
        get { return _scrollIntoViewObject; }
        private set
        {
            _scrollIntoViewObject = value;
            OnPropertyChanged("ScrollIntoViewObject");
        }
    }

    In your RadGridView:
    <telerik:RadGridView ItemsSource="{Binding MyItems}">
          <i:Interaction.Behaviors>
                <Utils:RadGridScrollIntoViewBehavior/>
          </i:Interaction.Behaviors>
    </telerik:RadGridView>

    And lastly:
    public class RadGridScrollIntoViewBehavior : Behavior<RadGridView>
    {
        private RadGridView Grid
        {
            get
            {
                return AssociatedObject;
            }
        }
     
        protected override void OnAttached()
        {
            base.OnAttached();
     
            Grid.DataContextChanged += GridDataContextChanged;
        }
     
        private void GridDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (e.OldValue != null)
            {
                ((INotifyPropertyChanged) e.OldValue).PropertyChanged -= DataContextOnPropertyChanged;
            }
            if (e.NewValue != null)
            {
                ((INotifyPropertyChanged) e.NewValue).PropertyChanged += DataContextOnPropertyChanged;
            }
        }
     
        private void DataContextOnPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (!e.PropertyName.Equals("ScrollIntoViewObject")) return;
     
            var propertyInfo = sender.GetType().GetProperty(e.PropertyName);
            var scrollIntoViewObject = propertyInfo.GetValue(sender, null);
     
            if(scrollIntoViewObject != null)
            {
                Grid.ScrollIntoView(scrollIntoViewObject);
            }
        }
    }

    There must be a better solution without the propertyname and the reflection, but this works in my scenario
  6. Lawrence
    Lawrence avatar
    10 posts
    Member since:
    Jul 2013

    Posted 10 Feb 2015 Link to this post

    Here's an Extension method that I wrote that will scroll into view and select the row with a data pager.

    public void ScrollIntoView<T>(T dataItem, RadDataPager radDataPager)
    {
        var items = (Collection<T>)this.ItemsSource;
        int index = items.IndexOf(dataItem);
        int page = (int)Math.Floor((double)index / radDataPager.PageSize);
     
        radDataPager.MoveToPage(page);
     
        this.SelectedItem = items[index];
        this.ScrollIntoView(this.SelectedItem);
    }
  7. MG
    MG avatar
    6 posts
    Member since:
    May 2013

    Posted 07 Jul 2015 Link to this post

    Based on Erik's post, here is another attempt..

    public class RadGridViewScrollIntoViewBehavior: Behavior<RadGridView>
        {
            private RadGridView Grid
            {
                get { return AssociatedObject; }
            }
     
            protected override void OnAttached()
            {
                base.OnAttached();
                Grid.Items.CollectionChanged += Items_CollectionChanged;
            }
     
            private void Items_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
            {
                if (e.Action == NotifyCollectionChangedAction.Add)
                {
                    var myRow = e.NewItems[0] as MyClassType;
                    if (labelSet != null)
                    {
                        Grid.ScrollIntoView(myRow );
                    }
                }
            }
        }

    In the RadGridView,

    <telerik:RadGridView ItemsSource="{Binding MyItems}">
          <i:Interaction.Behaviors>
                <Utils:RadGridScrollIntoViewBehavior/>
          </i:Interaction.Behaviors>
    </telerik:RadGridView>

     

    Thanks

  8. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 07 Jul 2015 Link to this post

    Hi,

    You can check the documentation on Scroll to a particular row or column on the different options RadGridView suggests and how the scroll methods are expected to work.

    Regards,
    Dimitrina
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  9. Dan
    Dan avatar
    22 posts
    Member since:
    Jun 2011

    Posted 01 Apr in reply to MG Link to this post

    public class RadGridViewScrollSelectedItemIntoViewBehavior : Behavior<RadGridView>
        {
            #region Overrides
            protected override void OnAttached()
            {
                base.OnAttached();
                AssociatedObject.Unloaded += OnUnloaded;
                AssociatedObject.SelectionChanged += OnSelectionChanged;
            }
     
             
            #endregion
     
            #region Implementation
            private void OnUnloaded(object sender, System.Windows.RoutedEventArgs e)
            {
                AssociatedObject.Unloaded -= OnUnloaded;
                AssociatedObject.SelectionChanged -= OnSelectionChanged;
            }
            private void OnSelectionChanged(object sender, SelectionChangeEventArgs e)
            {
                object si = AssociatedObject.SelectedItem;
                if (si != null)
                {
                    AssociatedObject.ScrollIntoView(si);
                }
            }
     
     
            #endregion
        }
  10. Stefan Nenchev
    Admin
    Stefan Nenchev avatar
    281 posts

    Posted 04 Apr Link to this post

    Hello Dan,

    Thank you for providing the additional approach and contributing to the Telerik community.

    Regards,
    Stefan Nenchev
    Telerik
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
Back to Top
UI for WPF is Visual Studio 2017 Ready