使用嵌套在 ScrollViewer 中的 ScrollBar 进行无限控制
有人可以向我解释为什么以下 XAML 不能按我期望的方式工作吗?是否有任何解决方法?
我希望 TextBox 尊重它使用的 RowDefinition 的 Min- 和 MaxHeight 属性。相反,它的 MaxHeight 用于遮盖可用空间,但同时它的内容在遮盖后面增长......呃?!
我期望的行为与您为 RowDefinition 提供非无限高度、2 个滚动条时的行为相同。一个滚动条用于文本框,另一个滚动条用于屏幕的其余部分。
<ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="40" />
<RowDefinition MinHeight="100" MaxHeight="200" />
<RowDefinition Height="40" />
</Grid.RowDefinitions>
<Button Content="Top" />
<TextBox Grid.Row="1" AcceptsReturn="True" xml:space="preserve" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
***Enter a lot of text including enters here!***
</TextBox>
<Button Content="Bottom" Grid.Row="2" />
</Grid>
</ScrollViewer>
希望有人能帮助我解决这个问题。
谢谢,
维姆
Could someone explain to me why the following XAML does not work the way I expect it to and if there are any workarounds for it?
I Expect the TextBox to respect the Min- and MaxHeight properties of the RowDefinition it uses. Instead it's MaxHeight is used to mask the available space, but at the sametime it's content is growing behind the mask... Ehhhh?!
The behavior I expect is the same as when you give the RowDefinition a non-infinite Height, 2 scrollbars. One ScrollBar for the TextBox and one for the rest of the screen.
<ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="40" />
<RowDefinition MinHeight="100" MaxHeight="200" />
<RowDefinition Height="40" />
</Grid.RowDefinitions>
<Button Content="Top" />
<TextBox Grid.Row="1" AcceptsReturn="True" xml:space="preserve" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
***Enter a lot of text including enters here!***
</TextBox>
<Button Content="Bottom" Grid.Row="2" />
</Grid>
</ScrollViewer>
Hope someone can help me with this problem.
Thanks,
Wim
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
设置文本框的最小和最大高度可以使滚动条正确显示。
Setting min and max height on the TextBox allows the scrollbars to appear correctly.
这确实是一些奇怪的行为。
似乎网格本身没有大小限制(位于 ScrollViewer 内部并且没有设置高度)和没有将高度设置为绝对值的 RowDefinition 的组合不能正确约束 TextBox。
如果你绝对需要这个星座并且不想依赖任何其他东西,你可以这样做:
This is indeed some strange behaviour.
Seems like a combination of the Grid itself having no size constraints (being inside a ScrollViewer and having no Height set) and the RowDefinition having no Height set to an absolute Value doesn't correctly constrain the TextBox.
If you absolutely need this constellation and don't want to depend on anything else you can do this: