c# - Reset WPF Datagrid scrollbar position -
when changing .datacontext property of datagrid (to new source) selected item gets cleared, scrollbar position retained. avoid call .scrollintoview(.item(0), after changing datacontext, move scrollbar upwards. displays wrong page fraction of second, , when scroll top before changing datacontext, have same problem.
so how can change .datacontext , resetting scrollbar position @ same time?
edit: should mention xaml looks this:
<datagrid virtualizingstackpanel.isvirtualizing="true" virtualizingstackpanel.virtualizationmode="recycling">
so maybe virtualizing cause.
have tried calling scrolltotop
scrollviewer
in datacontextchanged event?
<datagrid virtualizingstackpanel.isvirtualizing="true" virtualizingstackpanel.virtualizationmode="recycling" datacontextchanged="datagrid_datacontextchanged" ...> private void datagrid_datacontextchanged(object sender, dependencypropertychangedeventargs e) { scrollviewer scrollviewer = getvisualchild<scrollviewer>(datagrid); if (scrollviewer != null) { scrollviewer.scrolltotop(); } }
getvisualchild
private static t getvisualchild<t>(dependencyobject parent) t : visual { t child = default(t); int numvisuals = visualtreehelper.getchildrencount(parent); (int = 0; < numvisuals; i++) { visual v = (visual)visualtreehelper.getchild(parent, i); child = v t; if (child == null) { child = getvisualchild<t>(v); } if (child != null) { break; } } return child; }
Comments
Post a Comment