Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my ScrollViewer destroy my Grid Layout? WPF

Tags:

c#

.net

wpf

xaml

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.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>

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

like image 558
Martin Søberg Avatar asked Sep 07 '11 08:09

Martin Søberg


2 Answers

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.

like image 116
Kent Boogaart Avatar answered Oct 10 '22 22:10

Kent Boogaart


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.

like image 26
Martin Søberg Avatar answered Oct 10 '22 20:10

Martin Søberg