Setting HorizontalOffset of a StackPanel ...

I'm trying to programmatically set the HorizontalOffset property of a StackPanel (using SetHorizontalOffset() to do this). But the HorizontalOffset remains at 0. Is there something I need to do with the StackPanel to make this happen I've set the CanHorizontallyScroll property to true, and the Orientation property to Horizontal. The width is much wider than the viewable portion of the StackPanel, yet it doesn't work for me.

Could it somehow be related to the StackPanel's content (which is a grid)

-



Answer this question

Setting HorizontalOffset of a StackPanel ...

  • Xavier Espinoza

    Thanks, that explains it - and I've got it working now.

    Richard


  • le_sloth

    HorizontalOffset is a read only property that is exposed through the IScrollInfo interface of StackPanel.   This is only valid in a ScrollViewer scenario.   Instead setting the property you will have to call the method ScrollViewer.ScrollToHorizontalOffset().   This will cause the ScrollViewer to scroll to a desired horizontal offset if possible, then you can get that value with HorizontalOffset.

     

    HorizontalOffset value will differ when StackPanel.CanContentScroll is true or false.

     

    Scenario 1:

    With a layout similar to this

    <ScrollViewer>

                <StackPanel CanContentScroll=”true”>

                            <... your content ...>

                </StackPanel>

    </ScrollViewer>

    And suppose you call ScrollViewer.ScrollToHorizontalOffset(100)

    Logical scrolling will occur (per item), so the offset is measured by first item in view.

    StackPanel.HorizontalOffset == 100

    ScrollViewer.HorizontalOffset == 100

     

    Scenario 2:

    With a layout similar to this

    <ScrollViewer>

                <StackPanel CanContentScroll=”false”>

                            <... your content ...>

                </StackPanel>

    </ScrollViewer>

     

    And suppose you call ScrollViewer.ScrollToHorizontalOffset(100)

    Phisical scrolling will occur (per pixel)

    StackPanel.HorizontalOffset == 0

    ScrollViewer.HorizontalOffset == 100 (pixel value)

     

    Hope this helps.

     

    -- matt



  • Setting HorizontalOffset of a StackPanel ...