Search code examples
windows-phone-7xamlexpression-blend

How can I do this in Xaml(something like auto margin?)


Say I have 2 textblocks beside each other

TextBlock 1 - Hi how are you?
TextBlock 2 - I am fine thank you

Result

 Hi how are you? I am fine thank you

Now say I want to hide textblock 1(dynamcially). I would still see Textblock 2 but before it would be a huge amount of space where textblock 1 would be.

Result

                 I am fine thank you

I would like to move textblock2 automatically over.

Result

 I am fine thank you

This would also be the similar case if I dynamically changed TextBlock1 to be a shorter string of text and had both of them side by side, there would be lots of black space.


Solution

  • You should set column 0 's width to Auto, like this:

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <TextBlock Grid.Column="0" x:Name="textblock1" Text="Textblock1"></TextBlock>
        <TextBlock Grid.Column="1" x:Name="textblock2" Text="Textblock2">  </TextBlock>
    </Grid>