tags:

views:

275

answers:

1

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
actually, I can't use this solution. The solution should be included in xaml or ViewModel as I'm not using code behind
niao
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