This is a migrated thread and some comments may be shown as answers.

Scroll to row when programmatically setting selected item

13 Answers 2149 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Maria
Top achievements
Rank 1
Maria asked on 08 Dec 2010, 03:36 PM
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.

13 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 08 Dec 2010, 03:49 PM
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
0
Thomas
Top achievements
Rank 1
answered on 20 Jul 2011, 02:10 PM
The blog did not help me. Can you please give a example for this problem?

0
Erik
Top achievements
Rank 1
answered on 18 Aug 2012, 01:48 PM
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
0
Lawrence
Top achievements
Rank 2
Iron
answered on 10 Feb 2015, 02:33 PM
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);
}
0
MG
Top achievements
Rank 1
answered on 07 Jul 2015, 06:34 AM

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

0
Dimitrina
Telerik team
answered on 07 Jul 2015, 08:38 AM
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
0
Dan
Top achievements
Rank 1
answered on 01 Apr 2016, 02:22 PM
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
    }
0
Stefan Nenchev
Telerik team
answered on 04 Apr 2016, 01:17 PM
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.
0
Dinesh
Top achievements
Rank 1
answered on 07 Feb 2019, 06:08 AM
I just need some help on how to scroll HORIZONTALLY by using (SHIFT + MOUSE SCROLL) IN WPF application. Give some idea or Code & Project Solution....
0
Dinesh
Top achievements
Rank 1
answered on 08 Feb 2019, 09:38 AM
I just need some help on how to scroll HORIZONTALLY by using (SHIFT + MOUSE SCROLL) IN WPF application. Give some idea or Code & Project Solution...
0
Dinesh
Top achievements
Rank 1
answered on 08 Feb 2019, 09:43 AM
I just need some help on how to scroll HORIZONTALLY by using (SHIFT + MOUSE SCROLL) IN WPF application. Give some idea or Code & Project Solution...
0
Shanthosh
Top achievements
Rank 1
answered on 11 Feb 2019, 09:36 AM

using telerik not working on shift + mousewheel 

try this code without telerik

 private void ScrollViewer_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
        {  
             ScrollViewer scrollviewer = sender as ScrollViewer;

            if (Keyboard.Modifiers == ModifierKeys.Shift)
            {
                if (e.Delta > 0)
                    scrollviewer.PageLeft();
                else
                    scrollviewer.PageRight();
                e.Handled = true;
            }
        }

xaml code:

<ScrollViewer HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Visible" PreviewMouseWheel="ScrollViewer_PreviewMouseWheel" />

 

0
Vladimir Stoyanov
Telerik team
answered on 11 Feb 2019, 11:20 AM
Hello Dinesh,

I replied to your question in the other forum thread: https://www.telerik.com/forums/ctrl-mousescroll-for-zoom-mousescroll-for-scrolling#_sxWZEbW80ySdQ1Swtiruw.

Regards,
Vladimir Stoyanov
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
GridView
Asked by
Maria
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Thomas
Top achievements
Rank 1
Erik
Top achievements
Rank 1
Lawrence
Top achievements
Rank 2
Iron
MG
Top achievements
Rank 1
Dimitrina
Telerik team
Dan
Top achievements
Rank 1
Stefan Nenchev
Telerik team
Dinesh
Top achievements
Rank 1
Shanthosh
Top achievements
Rank 1
Vladimir Stoyanov
Telerik team
Share this question
or