增长的ScrollView(高度=“自动”MaxHeight=“拉伸”)
我想要什么: 我希望在我的 Silverlight 4 应用程序中有一个 ScrollView,它的高度与内容一起增长,但是如果滚动条会比容器的高度增长,则它会显示滚动条。
我找到的解决方案: 我发现很多问题的解决方案是拉伸滚动查看器,但这不是我想要的。滚动查看器应始终尽可能小。
我的问题: 为了使其在滚动查看器顶部变得更加困难,标题位于一个具有静态高度的堆栈面板。
解决方案1: 我首先使用纯 XAML 尝试过,但我不知道它应该如何工作。
<Grid Height="Auto" x:Name="myGrid" >
<Grid.RowDefinitions>
<RowDefinition Height="100"/>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Background="AliceBlue">
<!-- Dummy Header-->
</StackPanel>
<ScrollViewer Grid.Row="1" Height="Auto">
<Button Width="100" Height="50" Click="Button_Click" />
<!-- onClick the button will switch between height="600" and height="50"
Code:
private void Button_Click(object sender, RoutedEventArgs e)
{
if (sender is Button)
{
Button btn = (Button)sender;
btn.Height = (btn.Height == 50) ? 600 : 50 ;
}
}
-->
</ScrollViewer>
</Grid>
如果您单击该按钮,它会变得更高,并且滚动查看器将被剪切,因为它太高了。有什么建议吗?
解决方案2: 然后我尝试使用包含容器的实际高度设置 ScrollViewer 的 *max* 高度,因此我在 ScrollViewer 周围插入了一个 StackPanel。这在 VS2010 XAML 设计器中有效,但在代码执行中无效。我不知道为什么......
<Grid Height="Auto" x:Name="myGrid" >
<Grid.RowDefinitions>
<RowDefinition Height="100"/>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Background="AliceBlue">
<!-- Dummy Header-->
</StackPanel>
<StackPanel Grid.Row="1" x:Name="myStackPanel" Height="Auto" VerticalAlignment="Stretch">
<ScrollViewer Height="Auto" MaxHeight="{Binding ElementName=myStackPanel, Path=ActualHeight}">
<Button Width="100" Height="50" Click="Button_Click" />
<!-- onClick the button will switch between height="600" and height="50"
Code:
private void Button_Click(object sender, RoutedEventArgs e)
{
if (sender is Button)
{
Button btn = (Button)sender;
btn.Height = (btn.Height == 50) ? 600 : 50 ;
}
}
-->
</ScrollViewer>
</StackPanel>
</Grid>
提前致谢!
What I want:
I would like to have a ScrollView in my Silverlight 4 application which grows in Height together with the content, but does show a scrollbar if it would otherwise grow heigher than its container.
Solutions I found:
I found plenty of asked questions where the solution was to strech the Scrollviewer but that is not what I want. The Scrollviewer should always be as small as possible.
My Problem:
To make it a bit more difficult on top of the scrollviewer a header is located which is a stackpanel with static height.
Solution Approch 1:
I tried it firstly with plain XAML but I cant figure out how it should work.
<Grid Height="Auto" x:Name="myGrid" >
<Grid.RowDefinitions>
<RowDefinition Height="100"/>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Background="AliceBlue">
<!-- Dummy Header-->
</StackPanel>
<ScrollViewer Grid.Row="1" Height="Auto">
<Button Width="100" Height="50" Click="Button_Click" />
<!-- onClick the button will switch between height="600" and height="50"
Code:
private void Button_Click(object sender, RoutedEventArgs e)
{
if (sender is Button)
{
Button btn = (Button)sender;
btn.Height = (btn.Height == 50) ? 600 : 50 ;
}
}
-->
</ScrollViewer>
</Grid>
If You click on the button the it becomes higher and the Scrollviewer will be cut becaus it is to tall. Any sugestions?
Solution Approach 2:
Then I tried to set the *max*Height of the ScrollViewer with the actualHeight of the containing container, therefore I inseted a StackPanel around the ScrollViewer. This does work in the VS2010 XAML designer but not on code execution. I have no clue why...
<Grid Height="Auto" x:Name="myGrid" >
<Grid.RowDefinitions>
<RowDefinition Height="100"/>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Background="AliceBlue">
<!-- Dummy Header-->
</StackPanel>
<StackPanel Grid.Row="1" x:Name="myStackPanel" Height="Auto" VerticalAlignment="Stretch">
<ScrollViewer Height="Auto" MaxHeight="{Binding ElementName=myStackPanel, Path=ActualHeight}">
<Button Width="100" Height="50" Click="Button_Click" />
<!-- onClick the button will switch between height="600" and height="50"
Code:
private void Button_Click(object sender, RoutedEventArgs e)
{
if (sender is Button)
{
Button btn = (Button)sender;
btn.Height = (btn.Height == 50) ? 600 : 50 ;
}
}
-->
</ScrollViewer>
</StackPanel>
</Grid>
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决方案是适当使用
VerticalAlignment
。您希望包含滚动视图的网格行的大小为剩余空间的大小。您不希望 Scrollviewer 最初占据所有空间,但它需要限制在该空间内。Stretch
的默认VerticalAlignment
将占据所有可用大小。使用Top
会导致它只达到所需的高度,直到网格将其大小限制为可用空间。但请注意
VerticalScrollBarVisibility
,尽管您的文本中有指示,但您并未将其包含在您自己的 xaml 中。也许事实上这就是你一直以来真正想要的。就位后,删除VerticalAlignment
。根据场景的其余部分,您可能会发现实际上具有滚动查看器完整范围的轮廓边框更有意义。The solution is in the appropriate use of
VerticalAlignment
. You want the Grid row that contains the scrollview to be the size of the remaining space. You don't want the Scrollviewer to occupy all that space initially but it needs to be limited to that space. The defaultVerticalAlignment
ofStretch
would have occupy all the available size. UsingTop
instead will cause it to only be as high as it needs to be until the Grid limits it size to the available space.However note the
VerticalScrollBarVisibility
which you don't include in your own xaml despite indications in your text that it should. Perhaps in fact this is what you are really after all along. With that in place remove theVerticalAlignment
. Depending on the rest of your scenario you might find that actually have the outline border of the full extent of the scrollviewer makes more sense.