开发者

Why does my ScrollViewer destroy my Grid Layout? WPF

Problem: When adding a ScrollViwer around a Grid the Grid scaling is cancelled!

Eksampel: I have created a grid width 3 columns, the 1. coulymn should always be 2 times larger than column 2 and 3! Without the ScrollViewer this is always true, but when adding it it allows each column to decide its own size.

<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.RowDef开发者_运维问答initions>
        <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>

As you can clearly see the scaling factors are completely wrong! As the 2. column is way to large! and the 3. column is some random size...

Wrong Scaling factors

Any advice on this is well recieved.... Cheers Martin


You're asking the grid to assign a percentage of infinite space to each column. Infinite because horizontal scrolling is enabled on your ScrollViewer, and the whole point of ScrollViewers is to virtualize space. So what you're asking it to do doesn't even make sense.


Ok I see your point in why the column sizes a screwed.
But.. I thought of a solution as I read your posts...

As, Mohammed said, set a fixed width on my grid, well.. I want my grid to have same width as scrollviewer unless it gets to small, then I want the scrollviewer to take affect! So.. my solution is:

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>

(Only fixed for horizontal)

Thx.


The current setup is wrong, because the ScrollViewer does not limit the width and height of its child (i.e. unlimited), moreover, the Grid always fills all the available horizontal and vertical space available on its parent container, and that is why you see this weird behavior. You have to do one of the followings:

  1. either, remove the ScrollViewer as you mentioned.
  2. or, set a fixed height and width for your Grid.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜