DataGrid random behavior when scrolling
I came across a post in the silverlight forums which shows the random behavior of the DataGrid. This happens obviously only when scrollbars are there and when we scroll. Wraping DataGridRowsPresenter in the Template of the DataGrid in a scrollviewer seems to get around this issue. As scrollviewer has some border margin etc. I styled it to remove all unnecessary stuff.
<ScrollViewer Grid.ColumnSpan=”2″ Grid.Row=”1″ Style=”{StaticResource ScrollViewerStyle1}”>
<sdk:DataGridRowsPresenter x:Name=”RowsPresenter” />
</ScrollViewer>
If you remove the style applied to the DataGrid in the sample you can clearly see the random behavior. You can download the sample here .
I’m the guy who posted in the SL forum. This fix works like a charm. Thanks so much bro.
You are welcome.
Thanks a lot. This is great solution. But it kills ScrollIntoView method. Any idea how to fix that? Thank again
Hi,
Are you trying to scroll to a particular item or Top and Bottom. If later you can find the scrollviewer and use the Methods ScrolltoTop() etc. if scrolling to a Item, I am not sure.
Yes, I’m trying to scroll to a particular item.
CollectionViewGroup viewGroup = GetSelectedGroup();
if (viewGroup == null)
return;
dataGrid.ScrollIntoView(viewGroup, null);
dataGrid.ExpandRowGroup(viewGroup, true);
I will try to fix that issue in the source code of the DataGrid control.
Thanks.
The workaround Lee suggested is great, it resolve the wierd behaviour of the scrollbar. But I have the same problem as Alexander. Any clue how to fix this?
Right after I posted my previous comment, I found a solution. I read more into Lee’s suggestion. Here is what I did:
1. I added a name to the ScrollView (x:Name):
2. I had this extention method for other purposes. Basically this method finds some control, within another control.
public static T GetChildObject(this DependencyObject obj, string name) where T : DependencyObject
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
object c = VisualTreeHelper.GetChild(obj, i);
if (typeof(T).IsAssignableFrom(c.GetType()) && (String.IsNullOrEmpty(name) || ((FrameworkElement)c).Name == name))
{
return (T)c;
}
object gc = ((DependencyObject)c).GetChildObject(name);
if (gc != null)
return (T)gc;
}
return null;
}
3. Now I extract the ScrollView
ScrollViewer sv = dgDeviceUsers.GetChildObject(“svSomeName”);
4. Now you can use methods of ScrollView like ScrollToBottom() or ScrollToTop(). Eg. sv.ScrollToBottom();
It worked for me.