Ilkkka,
Here is the way to retain the scroll position after the reload. You have to add some namespaces, those are given below. To use those namespaces you should include the
UIAutomationProvider.dll and UIAutomationTypes.dll assemblies.
NameSpaces : using System.Windows.Automation.Peers;
using System.Windows.Automation.Provider;
Code : ........
/// <summary>
/// Getting the scroll percent value of the chart.
/// </summary>
/// <returns></returns>
private double GetVerticalScrollPercent()
{
AutomationPeer p;
if (this.fxgantt.UseChartVirtualization)
p = FrameworkElementAutomationPeer.FromElement(this.fxgantt.GanttChart.VerticalScrollViewerElement) ?? FrameworkElementAutomationPeer.CreatePeerForElement(this.fxgantt.GanttChart.VerticalScrollViewerElement);
else
p = FrameworkElementAutomationPeer.FromElement(this.fxgantt.GanttChart.VerticalScrollViewerElement) ?? FrameworkElementAutomationPeer.CreatePeerForElement(this.fxgantt.GanttChart.VerticalScrollViewerElement);
var chartScrollProvider = p.GetPattern(PatternInterface.Scroll) as IScrollProvider;
return chartScrollProvider.VerticalScrollPercent;
}
/// <summary>
/// The types of scrolling directions
/// </summary>
private enum ScrollDirection
{
Up,
Down,
Right,
Left
}
/// <summary>
/// Setting the scroll value to the grid.
/// </summary>
/// <param name="grid"></param>
/// <param name="direction"></param>
/// <param name="vp"></param>
private void SetVerticalScrollPercent(DataGrid grid, ScrollDirection direction, double vp)
{
// Get the scroll provider of the grid.
AutomationPeer p1 = FrameworkElementAutomationPeer.FromElement(grid) ?? FrameworkElementAutomationPeer.CreatePeerForElement(grid);
IScrollProvider scrollProvider = p1.GetPattern(PatternInterface.Scroll) as IScrollProvider;
// Scroll.
switch (direction)
{
case ScrollDirection.Up:
case ScrollDirection.Down:
if (scrollProvider.VerticallyScrollable)
{
scrollProvider.SetScrollPercent(System.Windows.Automation.ScrollPatternIdentifiers.NoScroll, vp);
}
break;
case ScrollDirection.Left:
case ScrollDirection.Right:
if (scrollProvider.HorizontallyScrollable)
{
scrollProvider.SetScrollPercent(vp, System.Windows.Automation.ScrollPatternIdentifiers.NoScroll);
}
break;
}
}
private void LoadGantt_Click(object sender, RoutedEventArgs e)
{
// Getting the scroll percent value.
double vp = this.GetVerticalScrollPercent();
this._ticks = DateTime.Now.Ticks;
this.fxgantt.ItemsSource = SampleProjectData.GetSampleData((int)this.rowCountUD.Value);
this.fxgantt.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Loaded, new Action(delegate()
{
// Update log right after the UI thread is free.
long elapsed = TimeComputingUtils.ToUtcKind(DateTime.Now).Ticks - this._ticks;
this.UpdateLoadLog((int)this.rowCountUD.Value, elapsed);
}));
// Setting the scroll percent to the grid.
DataGrid grid = ((DataGrid)this.fxgantt.FlexyTable.AsControl);
this.SetVerticalScrollPercent(grid, ScrollDirection.Up, vp);
}
..........
Thanks,
- Karthik.