2013-06-04 165 views
0

我正在開發一個基於XAML的應用程序,我有間距/佈局問題。我目前有我的主頁作爲1列網格。我有一個列表框,但我需要我的數據模板顯示在2列。第一個數據元素應該佔據屏幕的75%,第二個數據元素應該佔據剩下的部分。當我運行我的應用程序時,來自列表框項目的數據會聚集在屏幕的左側。我怎樣才能改變這種行爲?XAML網格佈局

<Grid x:Name="LayoutRoot"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto"></RowDefinition> 
     <RowDefinition Height="2*"></RowDefinition> 
     <RowDefinition Height="Auto"></RowDefinition> 
     <RowDefinition Height="Auto"></RowDefinition> 
     <RowDefinition Height="9*"></RowDefinition> 
    </Grid.RowDefinitions> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="12"/> 
     <ColumnDefinition Width="*" /> 
     <ColumnDefinition Width="12"/> 
    </Grid.ColumnDefinitions> 
    <!--My Section--> 
    <Grid Grid.Row="3" Grid.Column="1"> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition Height="Auto" /> 
     </Grid.RowDefinitions> 
     <TextBlock Grid.Row="0" Grid.Column="0" Text="Selected Location" FontWeight="Bold" Style="{StaticResource grayTextBox}"/> 
     <Line Grid.Row="1" Grid.Column="0" Stroke="White" StrokeThickness="1" /> 
     <ListBox Grid.Row="2" ItemsSource="{Binding Path=SelectedLocations, Mode=TwoWay}" HorizontalContentAlignment="Stretch" Visibility="{Binding Path=IsSelectedLocationAvailable, Converter={StaticResource visibilityConverter}}"> 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <Grid ShowGridLines="True"> 
         <Grid.RowDefinitions> 
          <RowDefinition Height="*" /> 
          <RowDefinition Height="*" /> 
          <RowDefinition Height="*" /> 
         </Grid.RowDefinitions> 
         <Grid.ColumnDefinitions> 
          <ColumnDefinition Width="*" /> 
          <ColumnDefinition Width="Auto" /> 
         </Grid.ColumnDefinitions> 
         <TextBlock Grid.Row="0" Grid.Column="0" Foreground="#ffb107" Text="{Binding Path=Name}" /> 
         <TextBlock Grid.Row="1" Grid.Column="0" Foreground="#ffb107" Text="{Binding Path=Address}" /> 
         <TextBlock Grid.Row="2" Grid.Column="0" Foreground="#ffb107" Text="{Binding Path=CityStateZip}" /> 
         <StackPanel Grid.Row="0" Grid.Column="1" Grid.RowSpan="3" Orientation="Horizontal" HorizontalAlignment="Right"> 
          <Rectangle Fill="Orange" /> 
          <Rectangle Fill="Blue" /> 
         </StackPanel> 
        </Grid> 

回答

0

爲什麼使用網格來居中內容?

目前,我有我的主要頁面爲1柱網

爲什麼要使用一個網格,如果它只有1列?在堆疊面板項目上使用邊距。

<Grid x:Name="LayoutRoot"> 
    <StackPanel Orientation="Vertical"> 
     <TextBlock Text="Selected Location" FontWeight="Bold" Style="{StaticResource grayTextBox}"/> 
     <Line Stroke="White" StrokeThickness="1" /> 
     <ListBox ItemsSource="{Binding Path=SelectedLocations, Mode=TwoWay}" HorizontalContentAlignment="Stretch" Visibility="{Binding Path=IsSelectedLocationAvailable, Converter={StaticResource visibilityConverter}}"> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <Grid ShowGridLines="True"> 
        <Grid.RowDefinitions> 
         <RowDefinition Height="*" /> 
         <RowDefinition Height="*" /> 
         <RowDefinition Height="*" /> 
        </Grid.RowDefinitions> 
        <Grid.ColumnDefinitions> 
         <ColumnDefinition Width="*" /> 
         <ColumnDefinition Width="Auto" /> 
        </Grid.ColumnDefinitions> 
        <TextBlock Grid.Row="0" Grid.Column="0" Foreground="#ffb107" Text="{Binding Path=Name}" /> 
        <TextBlock Grid.Row="1" Grid.Column="0" Foreground="#ffb107" Text="{Binding Path=Address}" /> 
        <TextBlock Grid.Row="2" Grid.Column="0" Foreground="#ffb107" Text="{Binding Path=CityStateZip}" /> 
        <StackPanel Grid.Row="0" Grid.Column="1" Grid.RowSpan="3" Orientation="Horizontal" HorizontalAlignment="Right"> 
         <Rectangle Fill="Orange" /> 
         <Rectangle Fill="Blue" /> 
        </StackPanel> 
       </Grid> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
     </ListBox> 
    </StackPanel> 
</Grid>