Why does my ScrollViewer destroy my grid layout? WPF

Problem: when adding ScrollViwer around the grid, the scaling of the grid is canceled!

Eksampel: I created a grid width of 3 columns, 1. coulymn should always be 2 times larger than columns 2 and 3! Without ScrollViewer, this is always true, but when added, 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! Since column 2. is suitable for large! and 3. the column is a random size ...

Invalid scaling factors

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

+8
c # wpf xaml
source share
3 answers

Ok, I see your point in why the column size is screwed.
But .. I was thinking of a solution when I read your messages ...

As Mohammed said, set a fixed width on my grid, well .. I want my grid to have the same width as the scrollviewer, if it doesn’t get small, then I want the scrollviewer to influence! 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 for horizontal)

thanks.

+3
source share

The current setting is incorrect because ScrollViewer does not limit the width and height of its child (i.e. unlimited), and the Grid always fills all the available horizontal and vertical space available in its parent container, and that is why you see this strange behavior. You must do one of the following:

  • uninstall ScrollViewer as you mentioned.
  • or set a fixed height and width for the Grid .
+5
source share

You ask the grid to assign a percentage of infinite space for each column. Endlessly, because horizontal scrolling is enabled on your ScrollViewer, and the whole point of ScrollViewers is virtualization of space. So you ask for it, it doesn't even make sense.

+5
source share

All Articles