Welcome to the Xceed Community | Help
Community Search  
More Search Options

How to set scroll offsets in DataGridControl?

Sort Posts: Previous Next
  •  05-20-2009, 5:39 AM Post no. 21055

    How to set scroll offsets in DataGridControl?

    Hi!

    In my application I have a navigation history (like web-browser). Each history step has his own set of data.
    In UI I have a DataGridControl with TableView. When user navigates forward or back in navigation history, ItemSource of DataGridControl changes to another object.

    I want to save the selection and scrolling offset. With selection I have no problem, but setting scroll offsets doesn't works.

    My code:

    private void ApplyData(DataLoaderResult dataLoaderResult)
    {
      //.. some code skipped ...  
    	
      DataGrid.ItemsSource = FDataView.PlainView;
      //.. some code skipped ...
      
      var scroll = (ScrollViewer)DataGrid.Template.FindName("PART_ScrollViewer", DataGrid);
    	
      scroll.ScrollToHorizontalOffset(horizontalScrollOffset);
      scroll.ScrollToVerticalOffset(verticalScrollOffset);
    }
    

    horizontalScrollOffset and verticalScrollOffset values are non-zero but scroll offset don't changes

    P.S. I use Xceed DataGrid v.3.1 Express

  •  05-20-2009, 7:07 AM Post no. 21056 in reply to 21055

    Re: How to set scroll offsets in DataGridControl?

    What you are doing is not wrong, but I found that I had to set the scroll offsets by dispatching the function call into another thread using a priority of Loaded (I think) as I don't have the code to hand.

    When I tried your approach I too found that my values were ignored - I think that they are reset when the grid reloads itself.

    If you still have problems let me know and I will post my code.

  •  05-20-2009, 11:42 AM Post no. 21060 in reply to 21056

    Re: How to set scroll offsets in DataGridControl?

    Yes,I still have this problem and don't know how to solve it.

    Help me please! :)

  •  05-25-2009, 12:51 AM Post no. 21159 in reply to 21060

    Re: How to set scroll offsets in DataGridControl?

    "Thanks to all" :)

    I have solved this problem by using the timer:

    private void ApplyData(DataLoaderResult dataLoaderResult)
    {
    // ... some code skipped ...

    DataGrid.ItemsSource = FDataView.PlainView;

    EnqueueDelayedSettingDataGridScroll();
    }

    private void EnqueueDelayedSettingDataGridScroll()
    {
    DispatcherTimer timer = new DispatcherTimer {
    Interval = TimeSpan.FromMilliseconds(1)
    };
    timer.Tick += delegate(object sender, EventArgs args)
    {
    timer.Stop();
    SetDataGridScrollAndSelection();
    };
    timer.Start();
    }

    private void SetDataGridScrollAndSelection()
    {
    if (FInitialDataGridParameters != null) {

    // ... some code skipped ...

    scroll.ScrollToHorizontalOffset(horizontalScrollOffset);
    scroll.ScrollToVerticalOffset(verticalScrollOffset);
    }
    }
  •  05-25-2009, 8:10 AM Post no. 21163 in reply to 21159

    Re: How to set scroll offsets in DataGridControl?

    Sorry for the delay - have been away for long weekend. The code that I would use is similar to yours (just does not involve the Timer). It does need .NET 3.5 SP1 to use the Dispatcher code.

    private void ApplyData(DataLoaderResult dataLoaderResult)
    {
     //Where you currently get the offsets call this...
            GetAndSetScrollViwerOffsets(DispatcherPriority.Loaded);
     
     // ... some code skipped ...
     DataGrid.ItemsSource = FDataView.PlainView;
    }

        internal void GetAndSetScrollViwerOffsets(DispatcherPriority dispatcherPriority)
        {
          double dVerticalOffset = 0.0;
          double dHorizontalOffset = 0.0;
          GetScrollViewerOffsets(ref dVerticalOffset, ref dHorizontalOffset);
          Dispatcher.BeginInvoke((Action)(() => SetScrollViewerOffsets(dVerticalOffset, dHorizontalOffset)), dispatcherPriority, null);
        }
       

        private void GetScrollViewerOffsets(ref double dVerticalOffset, ref double dHorizontalOffset)
        {
          dVerticalOffset = 0.0;
          dHorizontalOffset = 0.0;
          ScrollViewer sv = Template.FindName("PART_ScrollViewer", this) as ScrollViewer;
          if (sv != null)
          {
            dVerticalOffset = sv.VerticalOffset;
            dHorizontalOffset = sv.HorizontalOffset;
          }

        }


        private void SetScrollViewerOffsets(double dVerticalOffset, double dHorizontalOffset)
        {
          if (dVerticalOffset >= 0.0 || dHorizontalOffset >= 0.0)
          {
            ScrollViewer sv = Template.FindName("PART_ScrollViewer", this) as ScrollViewer;
            if (sv != null)
            {
              if (dVerticalOffset > sv.ScrollableHeight)
              {
                sv.ScrollToVerticalOffset(sv.ScrollableHeight);
              }
              else
              {
                sv.ScrollToVerticalOffset(dVerticalOffset);
              }
              if (dHorizontalOffset > sv.ScrollableWidth)
              {
                sv.ScrollToHorizontalOffset(sv.ScrollableWidth);
              }
              else
              {
                sv.ScrollToHorizontalOffset(dHorizontalOffset);
              }
            }
          }
        }

     

  •  05-27-2009, 1:54 AM Post no. 21207 in reply to 21163

    Re: How to set scroll offsets in DataGridControl?

    Thank you Derek
View as RSS news feed in XML
Contact | Site Map | Reviews | Legal Terms of Use | Trademarks | Privacy Statement Copyright 2011 Xceed Software Inc.