Reputation: 5070
Is there any way I can add grid column dynamically in xaml (e.g. using trigger/datatrigger)? The case is that I want to add/remove grid column depending on the Visibility property that is binded to that grid.
Upvotes: 0
Views: 1374
Reputation: 2552
Probably not an exact solution to your problem, but you could try something like this.
<Grid Name="theGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition Name="columnToHide" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<StackPanel Name="stackToHide" Grid.Column="0">
<Button>hello</Button>
</StackPanel>
<Button Grid.Column="1" Click="Button_Click">Bye</Button>
</Grid>
bool visible = true;
GridLength width;
GridLength height;
private void Button_Click(object sender, RoutedEventArgs e)
{
if (visible)
{
GridLength zero = new GridLength(0);
width = columnToHide.Width; //save original height and width
columnToHide.Width = zero; //make column invisible
visible = false;
}
else
{
columnToHide.Width = width; //restore original width
visible = true;
}
}
You could also try putting column content inside a container and changing the Visibility property on that, although that wouldn't cause any resizing of grid content, and you'd be left with space where the column used to be.
Upvotes: 1