Monday, October 29, 2012

How to solve this stretching issue in XAML?

How to solve this stretching issue in XAML?

I've got the following XAML (simplified):

<Grid x:Name="parentGrid">     <Grid.RowDefinitions>         <RowDefinition/>         <RowDefinition/>         <RowDefinition/>         <RowDefinition/>     </Grid.RowDefinitions>      <Grid Grid.Row="0">         <!-- content which fits its parent grid -->     </Grid>      <Grid Grid.Row="1">         <!-- content which fits its parent grid -->     </Grid>      <Grid Grid.Row="2">         <!-- content which fits its parent grid -->     </Grid>      <Grid Grid.Row="3">         <!-- content which fits its parent grid -->     </Grid> </Grid> 

That's a configurable container, which holds one to four of our dialogs. This container is reused a lot and the amount of containing dialogs always differs.

The requirement is, that if there is just one dialog (so just the first grid is filled), it should stretch over the whole parent grid. If there are two grids filled, each container should fill the half of the parent grid. If there are three... and so forth.

I couldn't get it work with neither <RowDefinition Height="Auto"/> (default anyway) nor <RowDefinition Height="*"/>. E.g if there is just one Grid filled, it doesn't fit the whole parent grid. If I remove three RowDefinitions, it works though.

Additional information: the non-filled grids visibility is always set to Visibility.Collapsed.

Answers & Comments...

Answer: 1

I've always found that if you want to hide a column or row that you bind a property to it like so:

<Grid x:Name="ParentGrid">     <Grid.RowDefinitions>         <RowDefinition Height="{Binding GridRow0}" />         <RowDefinition Height="{Binding GridRow1}" />         <RowDefinition Height="{Binding GridRow2}" />         <RowDefinition Height="{Binding GridRow3}" />     </Grid.RowDefinitions> <-- grid contents --> </Grid> 

Setting the properties like:

Public Property GridRow0 as GridLength = New GridLength(GridUnitType.Star) Public Property GridRow1 as GridLength = New GridLength(GridUnitType.Star) Public Property GridRow2 as GridLength = New GridLength(GridUnitType.Star) Public Property GridRow3 as GridLength = New GridLength(GridUnitType.Star) 

And then when you figure out how many rows are going to be needed, you do something like pass the count to a procedure:

Public Sub FixRows(count as Integer)     Select Case count         Case 1             GridRow3 = New GridLength(0)             GridRow2 = New GridLength(0)             GridRow1 = New GridLength(0)             GridRow0 = New GridLength(GridUnitType.Star)         Case 2             GridRow3 = New GridLength(0)             GridRow2 = New GridLength(0)             GridRow1 = New GridLength(GridUnitType.Star)             GridRow0 = New GridLength(GridUnitType.Star)         Case 3             GridRow3 = New GridLength(0)             GridRow2 = New GridLength(GridUnitType.Star)             GridRow1 = New GridLength(GridUnitType.Star)             GridRow0 = New GridLength(GridUnitType.Star)         Case 4             GridRow3 = New GridLength(GridUnitType.Star)             GridRow2 = New GridLength(GridUnitType.Star)             GridRow1 = New GridLength(GridUnitType.Star)             GridRow0 = New GridLength(GridUnitType.Star)         Case Else             'Whatever is needed     End Select End Sub 
by : Barry Franklinhttp://stackoverflow.com/users/680563




No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog