2009-04-24 350 views
55

有什麼辦法如何告訴組件在WPF採取可用空間的100%?如何設置寬度爲100%,在WPF

width: 100%; 

在CSS

我有了這個XAML,我不知道如何強制電網採取100%的寬度。

<ListBox Name="lstConnections"> 
    <ListBox.ItemTemplate> 
    <DataTemplate> 
     <Grid Background="LightPink"> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="Auto"/> 
      <ColumnDefinition Width="Auto"/> 
     </Grid.ColumnDefinitions> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition Height="Auto"/> 
     </Grid.RowDefinitions> 
     <TextBlock Grid.Row="0" Grid.Column="0" Text="{Binding Path=User}" Margin="4"></TextBlock> 
     <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Path=Password}" Margin="4"></TextBlock> 
     <TextBlock Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" Text="{Binding Path=Host}" Margin="4"></TextBlock> 
     </Grid> 
    </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

結果看起來像

alt text http://foto.darth.cz/pictures/wpf_width.jpg

我做到了粉紅色所以很明顯有多少空間沒有考慮。我需要使粉紅色的網格100%的寬度。

+0

Link是死了... – 2017-03-10 14:02:18

+0

@MartiniBianco我很抱歉,但這個問題是8歲。我甚至不記得照片中的內容,從那時起我也沒有使用過WPF。 – 2017-03-11 13:01:50

回答

70

它是Grid的是在它的寬度施加在容器上。在這種情況下,這是一個ListBoxItem,默認情況下它是左對齊的。你可以將它設置爲伸展如下:

<ListBox> 
    <!-- other XAML omitted, you just need to add the following bit --> 
    <ListBox.ItemContainerStyle> 
     <Style TargetType="ListBoxItem"> 
      <Setter Property="HorizontalAlignment" Value="Stretch"/> 
     </Style> 
    </ListBox.ItemContainerStyle> 
</ListBox> 
43

你可以使用HorizontalContentAlignment="Stretch"如下:

<ListBox HorizontalContentAlignment="Stretch"/> 
相關問題