简体   繁体   English

如何在Windows Phone 8.0中使用网格列在左右两侧设置项目

[英]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: 您可以在网格中设置元素的HorizontalAlignment

<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. 如果这是页面上的唯一内容,则可能还需要同时设置TextBlock和Image的垂直对齐方式。 As then the content might come in the center of the page. 届时内容可能会出现在页面中心。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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