ScrollViewer and Stretch


  • I have the following XAML:
    <ScrollViewer x:Name="scrHorizontalStackPanel" Grid.Column="1" Grid.Row="4" Grid.ColumnSpan="2" HorizontalScrollMode="Auto" HorizontalScrollBarVisibility="Hidden" VerticalScrollMode="Disabled" VerticalScrollBarVisibility="Hidden">
    	<ItemsControl x:Name="itmHorizontalStackPanel" ItemTemplate="{StaticResource RandomItemData}">
    		<ItemsControl.ItemsPanel><ItemsPanelTemplate><StackPanel Background="Silver" Orientation="Horizontal"/></ItemsPanelTemplate></ItemsControl.ItemsPanel>
    But when I use this code, the ScrollViewer grows to the full size necessary to display all the items in the ItemsControl, so most of it is off the page and it does not scroll because it is already showing all of it's content (at least it thinks it is, even though I can't see it since it's off the screen). I want the ScrollViewer to stretch to the left & right edges of the screen (but obviously no more). With most other elements you would just set the HorizontalAlignment property to Stretch, but that does not seem to be working here. It works if I specify a literal value for width, but I need to dynamically calculate the width. I thought about doing this in the codebehind, but I was hoping there would be a way to do it using XAML. Can anybody help me here? Thanks.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Wednesday, April 08, 2015 9:36 PM

All replies

  • Set the height of the scrollviewer so the control will know when to scroll
    Thursday, April 09, 2015 12:33 AM
  • That is exactly what I want to not do. I want the height (ok, I said width in my post, but the same thing applies) to be calculated just like when other elements are used in the layout, because the layout changes at certain points while being used, and is obviously not the same on everybody's display. I want the height or width to be dynamically calculated. Is there any easy way to do this without continuously measuring and remeasuring all the other elements? Is it possible to put some kind of wrapper in it's place (that I do not need to specify a width and/or height for) that will contain the ScrollViewer? Recalculating the width and/or height every time an element is modified, added, or removed seems very inefficient. Any ideas? Thanks.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Thursday, April 09, 2015 1:35 AM