wpf UWP 网格中的内部项目不拉伸 XAML
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32344120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Inner Items in UWP Grid not stretching XAML
提问by Tilcey
Having an issue with my items in this list view not spanning the width of the screen. Sorry if my code is sloppy. A bit new at this. I do have similar pages with similar code.
我在此列表视图中的项目不跨越屏幕宽度出现问题。对不起,如果我的代码草率。这方面有点新。我确实有类似代码的类似页面。
Thanks in advance!
提前致谢!
Not the full code - wasn't sure if you would need it.
不是完整的代码 - 不确定您是否需要它。
<ListView x:Name="listView" DataContext="{Binding ServicesCvs}" ItemsSource="{Binding}" Margin="0,80,0,0" HorizontalContentAlignment="Stretch">
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<Button Command="{Binding DataContext.SelectServiceCommand, ElementName=serviceSelectionPage}"
CommandParameter="{Binding Text, ElementName=ServiceIdTextBlock}">
<StackPanel Orientation="Horizontal" Margin="0,0,0,0">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBlock x:Name="ServiceIdTextBlock" Grid.Column="1" FontFamily="Arial" Text="{Binding ServiceId}" Style="{StaticResource TitleTextBlockStyle}" TextWrapping="NoWrap" Foreground="#FF18417C" Margin="10,0,0,0"/>
<TextBlock x:Name="ServiceType" Grid.Column="2" FontFamily="Arial" Text="{Binding ServiceTypeId}" Style="{StaticResource TitleTextBlockStyle}" TextWrapping="NoWrap" Foreground="#FF414B59"/>
<TextBlock x:Name="AssetId_TextBlock" Grid.Column="3" FontFamily="Arial" Text="{Binding AssetId}" Style="{StaticResource TitleTextBlockStyle}" TextWrapping="NoWrap" Foreground="#FF4D7CC1"/>
<TextBlock x:Name="ProductItemId_TextBlock" Grid.Column="4" FontFamily="Arial" Text="{Binding ProductItemId}" Style="{StaticResource TitleTextBlockStyle}" TextWrapping="NoWrap" Foreground="#FF414B59"/>
</Grid>
</StackPanel>
</Button>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
回答by Ive
Put this in your ListView, it will works in Designer too
把它放在你的 ListView 中,它也可以在 Designer 中使用
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
</ListView.ItemContainerStyle>
And as Mark Feldman said, remove that StackPanel and set button's HorizontalContentAlignment="Stretch".
正如 Mark Feldman 所说,移除 StackPanel 并设置按钮的 HorizontalContentAlignment="Stretch"。
回答by SelAromDotNet
i'm not sure if this is the same problem you're having, but one of the things that drove me crazy about listview/gridview is that the containerswouldn't stretch. the templates would stretch, but only as far as the container, which by default, does NOT stretch.
我不确定这是否与您遇到的问题相同,但让我对 listview/gridview 感到疯狂的一件事是容器不会伸展。模板会拉伸,但仅限于容器,默认情况下不会拉伸。
the fix is to override the style for the ListViewItem/GridViewItem by adding something like this to your resources (either on the page or in the app.xaml resource dictionary:
解决方法是通过向您的资源(在页面上或在 app.xaml 资源字典中)添加类似内容来覆盖 ListViewItem/GridViewItem 的样式:
<Style TargetType="ListViewItem">
<Setter Property="HorizontalAlignment" Value="Stretch" />
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
you won't see the results in the designer (!!!) but when you run it, it should stretch the contents of the item to fill the screen
你不会在设计器中看到结果 (!!!) 但是当你运行它时,它应该拉伸项目的内容以填满屏幕
i hope this is helpful!
我希望这是有帮助的!
回答by Mark Feldman
Two issues:
两个问题:
- Set your button's
HorizontalContentAlignment="Stretch". Your child controls may specify stretch but they will only stretch into whatever space the parent makes available to them. - Remove the horizontal StackPanel. A StackPanel overrides stretching and effectively says "reserve as much space as the child controls require for layout and no more".
- 设置按钮的
HorizontalContentAlignment="Stretch". 您的子控件可以指定拉伸,但它们只会拉伸到父控件提供给它们的任何空间。 - 移除水平 StackPanel。StackPanel 覆盖拉伸并有效地表示“保留子控件布局所需的空间,仅此而已”。

