Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Override ScrollViewer size
Message
From
10/04/2009 03:53:49
 
 
To
09/04/2009 14:30:38
General information
Forum:
ASP.NET
Category:
Windows Presentation Foundation (WPF)
Environment versions
Environment:
C# 3.0
Miscellaneous
Thread ID:
01393942
Message ID:
01394095
Views:
30
>>Hi,
>>This should be simple but it's beating me. Here's a simple XAML window:
<Window x:Class="WpfApplication1.Window23"
>>    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
>>      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>>      Title="ScrollViewer Test">
>>    <StackPanel >
>>        <ScrollViewer  HorizontalScrollBarVisibility="Auto">
>>            <Rectangle Width="500" Height="500" Fill="Red" />
>>        </ScrollViewer>
>>    </StackPanel>
>></Window>
>>When I run this and make the window narrower then the horizontal scrollbar kicks in when required. If, however, I reduce the height of the window then the minimum height of the StackPanel (and ScrollViewer) is constrained by the height of the Rectangle (if I switch the Orientation of the StackPanel I get the opposite behaviour (i.e vertical Scrollbar works) )
>>
>>I assume the solution to this may be to override Measure/Arrange somewhere - or maybe to wrap the ScrollViewer in something other than a StackPanel - but can't seem to find anything that will work :-{
>>
>>UPDATE: Ha, DockPanel works. But it would be good to find a solution when using StackPanel.....
>
>Grid also works, and in this case, no parent also works.
>
>From what I've seen, StackPanel's always respect the sizing requests of their child objects in the direction of flow, so they do not make good parents for a dynamically sized ScrollViewer.
>
>Here's my solution:
>
>
><Window x:Class="WpfApplication2.Window1"
>    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
>      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>      Title="ScrollViewer Test"
>        Name="myWindow">
>  <Grid Name="myGrid">
>    <StackPanel>
>      <ScrollViewer  HorizontalScrollBarVisibility="Auto" Height="{Binding Path=ActualHeight, ElementName=myGrid  }" >
>        <Rectangle Width="500" Height="500" Fill="Red" />
>      </ScrollViewer>
>    </StackPanel>
>  </Grid>
></Window>
>
>
>That's probably is not worth using, but it show one way to work around it. The basic fix is to set the height for the scroll viewer to the desired size. You could also add a converter to the binding if for instance you wanted the scroll viewer to take up 30% of the space of the grid.

Thank you. That works well (I think I tried something similar that didn't work and moved on :-( )
I also tried this with a DockPanel instead of the Grid. It worked OK with the Binding - but not without. However, on my real form the ScrollViewer has no binding but still has both scrollbars. I guess I need to work out what the difference is - but, hey, it's working so...

Oh, I also tried the above without the Grid (binding to the Window.ActualHeight). That gave the required vertical scroll bar behaviour - but the horizontal scrollbar disappeared. Haven't figured out why that might be.....
Best,
Viv
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform