2014-01-05 84 views
14

我有一個由3列組成的Windows Phone/XAML Grid。特別是,我希望第三列與屏幕的右側對齊。將柵格列對齊

<Grid Background="Transparent" Margin="0,3"> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="Auto"/> 
      <ColumnDefinition Width="*"/> 
      <ColumnDefinition Width="Auto"/> 
     </Grid.ColumnDefinitions> 

    <Image Grid.Column="0" x:Name="Marker" Width="60" Height="60" VerticalAlignment="Center" Stretch="Uniform" HorizontalAlignment="Center"/> 
    <TextBlock Grid.Column="1" x:Name="Name" TextAlignment="Left" VerticalAlignment="Center" Margin="20,0" /> 
    <Image Grid.Column="2" x:Name="Selected" Width="48" Height="48" VerticalAlignment="Center" Stretch="Uniform" HorizontalAlignment="Center"/> 
</Grid> 

結果,取而代之的,是這樣的:

enter image description here

當它應該是這樣的:

enter image description here

+0

固定後有沒有'的Horizo​​ntalAlignment = 「右」'? –

+1

是的,它不工作:( – TheUnexpected

+0

這個網格是'ListBox'的ItemTemplate'的一部分,因爲我看不到Grid中的其他項目 –

回答

18

正如您所提及的ListBox的ItemTemplate,您可以做的是將HorizontalContentAlignment設置爲Stretch

<ListBox> 
     <ListBox.ItemContainerStyle> 
      <Style TargetType="ListBoxItem"> 
       <Setter Property="HorizontalContentAlignment" Value="Stretch"/> 
      </Style> 
     </ListBox.ItemContainerStyle> 
    </ListBox> 
2

試試這個:

<Grid Background="Transparent" Margin="0,3"> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="Auto"/> 
      <ColumnDefinition Width="*"/> 
     </Grid.ColumnDefinitions> 
    <StackPanel Orientation="Horizontal"> 
     <Image x:Name="Selected" Width="48" Height="48" VerticalAlignment="Center" Stretch="Uniform" HorizontalAlignment="Center"/> 
     <TextBlock x:Name="Name" TextAlignment="Left" VerticalAlignment="Center" Margin="20,0" /> 
    </StackPanel> 
    <Image Grid.Column="1" x:Name="Selected" Width="48" Height="48" VerticalAlignment="Center" Stretch="Uniform" HorizontalAlignment="Right"/> 
</Grid> 
1

在網格中嘗試HorizontalAlignment="Stretch"

+0

使用ListView而不是ListBox可能會使多列列表變得更加複雜。 – RudolfJan

0

我有同樣的問題,它被移除的StackPanel