如何在 WPF 中将宽度设置为 100% [英] How to set width to 100% in WPF
本文介绍了如何在 WPF 中将宽度设置为 100%的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有什么方法可以告诉 WPF 中的组件占用 100% 的可用空间?
Is there any way how to tell component in WPF to take 100% of available space?
喜欢
width: 100%;
在 CSS 中
我有这个 XAML,但我不知道如何强制 Grid 使用 100% 宽度.
I've got this XAML, and I don't know how to force Grid to take 100% width.
<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>
结果看起来像
替代文字 http://foto.darth.cz/pictures/wpf_width.jpg
我将它设为粉红色,因此很明显它占用了多少空间.我需要将粉色网格设为 100% 宽度.
I made it pink so it's obvious how much space does it take. I need to make the pink grid 100% width.
推荐答案
它是 Grid
的容器,它在其宽度上施加影响.在本例中,这是一个 ListBoxItem
,默认情况下是左对齐的.您可以将其设置为拉伸如下:
It is the container of the Grid
that is imposing on its width. In this case, that's a ListBoxItem
, which is left-aligned by default. You can set it to stretch as follows:
<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>
这篇关于如何在 WPF 中将宽度设置为 100%的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文