Getting three columns have the same width is done by setting Width
to Auto
.
<Grid x:Name="myGrid">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0">One</Label>
<Label Grid.Row="0" Grid.Column="1" x:Name="label1">Two</Label>
<Label Grid.Row="0" Grid.Column="2">Three</Label>
</Grid>
I want to achieve that if middle column is collapsed or hidden that the other two take up remaining space and get equal width.
If I just set Visibility to Collapsed or Hidden because of the Width="*", the other two columns width stays the same.
<Label Grid.Row="0" Grid.Column="1" Visibility="Collapsed">Two</Label>
I achieved desired functionality by programatically setting second column width to auto, but am looking for some other solution (preferably xaml way one).
private void Button_Click(object sender, RoutedEventArgs e)
{
this.myGrid.ColumnDefinitions[1].Width = GridLength.Auto;
this.label1.Visibility = Visibility.Collapsed;
}
I added Xaml Binding as suggested by Rob like this:
Code behind xaml: