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.
A:
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.
MoominTroll
2009-11-25 14:54:40
actually, I can't use this solution. The solution should be included in xaml or ViewModel as I'm not using code behind
niao
2009-11-26 07:00:12
Ah. In that case I'm not sure I can help, sorry. Any reason you don't want to indulge in a code behind, out of curiosity?
MoominTroll
2009-11-26 09:11:24