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
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


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

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);
}

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
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

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
}
Thank you for providing the additional approach and contributing to the Telerik community.
Regards,
Stefan Nenchev
Telerik




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" />
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