Make WPF / SL grid ignore child when sizing

I have a Grid that has several children, one of which is ScrollViewer. I want the Grid itself to be determined based on all its children except ScrollViewer, and I just want to tackle the space that is created by determining the size of the Grid for the rest of its children. (For example, Grid is 2x2, and ScrollViewer is in row 0, column 0, and so the other three Grid elements are enough to determine the size of the grid.)

Is there a good way to do this? I studied creating a custom panel either to replace the Grid or to host a ScrollViewer, but under no circumstances do I ever get a call during MeasureOverride / ArrangeOverride calls that can tell me about the final width / height of the grid line / column I'm curious (e.g. row 0, column 0).

It seemed to me that I had to extract from the Grid and call the base MeasureOverride / ArrangeOverride, but remove the ScrollViewer from the Children of the Grid right before the call (and put it back), but messing with the visual tree while calculating the layout seems like a bad idea.

Here is an example:

<Grid x:Name="LayoutRoot" Background="White"> <Grid VerticalAlignment="Top" HorizontalAlignment="Left"> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition/> </Grid.ColumnDefinitions> <ScrollViewer Grid.Row="0" Grid.Column="0" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto"> <Button Height="300" Width="300"/> </ScrollViewer> <Button Grid.Row="1" Grid.Column="0" Height="100" Width="100" Content="1,0"/> <Button Grid.Row="0" Grid.Column="1" Height="100" Width="100" Content="0,1"/> <Button Grid.Row="1" Grid.Column="1" Height="100" Width="100" Content="1,1"/> </Grid> </Grid> 

I would like the grid size not to change as the button size in ScrollViewer changes, for example, I want the Grid to provide the ScrollViewer with a 100x100 square, which is determined by the rest of the contents of the Grid. And if I changed each of the 3 100x100 buttons to 200x200, I would like ScrollViewer to get 200x200, etc.

Pavel’s example brings me to the nearest moment, with the corresponding rows of rows / columns of the Grid, but ScrollViewer does not adapt to the size given to it when calling Arrange. See below:

Image showing effect of Pavlo's NoSizeDecorator

+3
source share
1 answer

If I understand correctly what you want, then you can do the next trick. Create a decorator that will request 0 spaces during the “Measurement” stage and will suit the child with all the given space at the “Arrange” stage:

 public class NoSizeDecorator : Decorator { protected override Size MeasureOverride(Size constraint) { // Ask for no space Child.Measure(new Size(0,0)); return new Size(0, 0); } } 

And your XAML will look like this:

 <Grid x:Name="LayoutRoot" Background="White"> <Grid VerticalAlignment="Top" HorizontalAlignment="Left"> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition/> </Grid.ColumnDefinitions> <my:NoSizeDecorator Grid.Row="0" Grid.Column="0"> <ScrollViewer ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto"> <Button Height="300" Width="300" Content="0,0"/> </ScrollViewer> </my:NoSizeDecorator> <Button Grid.Row="1" Grid.Column="0" Height="100" Width="100" Content="1,0"/> <Button Grid.Row="0" Grid.Column="1" Height="100" Width="100" Content="0,1"/> <Button Grid.Row="1" Grid.Column="1" Height="100" Width="100" Content="1,1"/> </Grid> </Grid> 
+6
source

Source: https://habr.com/ru/post/1480901/


All Articles