简体   繁体   中英

How to set items on left and right with grid column in windows phone 8.0

Can anyone say how to do like that:

In a pop-up I have two items one textblock and one image, in function of size of text I need to put all times text on left and icon right, like in the second picture.

 <Grid>
    <Grid.ColumnDefinitions>
       <ColumnDefinition Width="Auto"/>
       <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
         <TextBlock Grid.Column="0" Text="TEXT"/>
         <Image Grid.Column="1" />
 </Grid>

First Image is like that when first column is set auto: 在此处输入图片说明

When set an static width:

 <Grid>
        <Grid.ColumnDefinitions>
           <ColumnDefinition Width="400"/>
           <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
             <TextBlock Grid.Column="0" Text="TEXT"/>
             <Image Grid.Column="1" />
     </Grid>

After set

在此处输入图片说明

I want to set dynamic, not with a static value in width.

You can set the HorizontalAlignment of the element in the grid:

<Grid HorizontalAligment="Stretch">
    <Grid.ColumnDefinitions>
       <ColumnDefinition Width="Auto"/>
       <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
         <TextBlock Grid.Column="0" Text="TEXT"/>
         <Image Grid.Column="1" HorizontalAlignment="Right" />
</Grid>

This should put the image on the right hand side of the column. You might find you have to tell the grid to stretch to fill it's container as well.

Alternatively, assuming that your icons are the same sizes, you could swap the column definitions:

<Grid HorizontalAligment="Stretch">
    <Grid.ColumnDefinitions>
       <ColumnDefinition Width="*"/>
       <ColumnDefinition Width="Auto"/>
    </Grid.ColumnDefinitions>
         <TextBlock Grid.Column="0" Text="TEXT"/>
         <Image Grid.Column="1" />
</Grid>

so that the second column takes it width from its content (the icons) and the first stretches to fill the available space.

With this sort of problem you often have to try two or three things to get the exact behaviour you're looking for.

Try this

<Grid>
    <Grid.ColumnDefinitions>
       <ColumnDefinition Width="*"/>
       <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
         <TextBlock Grid.Column="0" Text="TEXT" HorizontalAlignment="Left"/>
         <Image Grid.Column="1" HorizontalAlignment="Right"/>
 </Grid>

You might also need to set the vertical alignment for both TextBlock and Image if this is the only content on the page. As then the content might come in the center of the page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM