为什么我的ScrollViewer会破坏我的网格布局? WPF

时间:2011-09-07 08:37:35

标签: c# .net wpf xaml

问题:在网格周围添加ScrollViwer时,网格缩放被取消了!

Eksampel: 我创建了一个网格宽度为3列,1。coulymn应该总是比第2列和第3列大2倍! 没有ScrollViewer,这总是正确的,但是在添加它时,它允许每列确定自己的大小。

<Window x:Class="alternatingGridRow.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="200" Width="Auto" Loaded="WindowLoaded">
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Disabled">
    <Grid x:Name="LayoutRoot" ShowGridLines="True">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" MinHeight="23" MaxHeight="60"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="2*"/>
            <ColumnDefinition Width="1*"/>
            <ColumnDefinition Width="1*"/>
        </Grid.ColumnDefinitions>
            <TextBlock HorizontalAlignment="Stretch" Text="sdasdasdasdsadsadasddasdck" TextWrapping="Wrap" VerticalAlignment="Top" />
            <TextBlock Foreground="Red" Grid.Column="1" HorizontalAlignment="Stretch" Text="sdasdasdasdsadsadasddasdck" TextWrapping="Wrap" VerticalAlignment="Top" />
    </Grid>
</ScrollViewer>

您可以清楚地看到缩放因子完全错误!由于2.列是大的方式! 3.列是一些随机大小...

Wrong Scaling factors

对此的任何建议都很好收到.... 干杯马丁

3 个答案:

答案 0 :(得分:5)

当前设置错误,因为ScrollViewer不限制其子项的宽度和高度(即无限制),此外,Grid始终填充所有可用的水平和垂直空间它的父容器,这就是你看到这种奇怪行为的原因。您必须执行以下操作之一:

  1. 或者,如您所述删除ScrollViewer
  2. 或者,为Grid设置固定的高度和宽度。

答案 1 :(得分:5)

您要求网格为每列分配无限空间的百分比。无限,因为在ScrollViewer上启用了水平滚动,ScrollViewers的整个要点是虚拟化空间。所以你要求它做的事情甚至没有意义。

答案 2 :(得分:4)

好的,我明白了为什么柱子的尺寸变硬了 但是......当我阅读你的帖子时,我想到了一个解决方案......

正如,穆罕默德所说,在我的网格上设置一个固定宽度,好吧..我希望我的网格与滚动查看器具有相同的宽度,除非它变小,然后我希望滚动查看器生效! 所以..我的解决方案是:

MinWidth="500" Width="{Binding ActualWidth, ElementName=scrollviewer}"

<Window x:Class="alternatingGridRow.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="200" Width="Auto">
<ScrollViewer x:Name="scrollviewer" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Disabled">
    <Grid x:Name="LayoutRoot" ShowGridLines="True" MinWidth="500" Width="{Binding ActualWidth, ElementName=scrollviewer}">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" MinHeight="23" MaxHeight="60"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="2*"/>
            <ColumnDefinition Width="1*"/>
            <ColumnDefinition Width="1*"/>
        </Grid.ColumnDefinitions>
            <TextBlock HorizontalAlignment="Stretch" Text="sdasdasdasdsadsadasddasdck" TextWrapping="Wrap" VerticalAlignment="Top" />
            <TextBlock Foreground="Red" Grid.Column="1" HorizontalAlignment="Stretch" Text="sdasdasdasdsadsadasddasdck" TextWrapping="Wrap" VerticalAlignment="Top" />
    </Grid>
</ScrollViewer>

</Window>

(仅适用于横向)

THX。

相关问题