2009-04-24 176 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"/> 
相关问题