gpt4 book ai didi

c# - UWP 拉伸(stretch) GridView

转载 作者:行者123 更新时间:2023-11-30 14:51:02 25 4
gpt4 key购买 nike

如何水平拉伸(stretch) GridViewItem?尝试在 ItemContainerStyle 中设置属性 Horizo​​ntalContentAlignment 或样式属性。这没有帮助。

App example image

代码如下:

<GridView ItemsSource="{x:Bind Banks}" SelectionMode="None" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
<GridView.ItemContainerStyle>
<Style TargetType="GridViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
</GridView.ItemContainerStyle>
<GridView.ItemTemplate>
<DataTemplate x:DataType="model:Bank">
<Grid HorizontalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<TextBlock Text="{Binding Name}" Grid.Column="0" />

<GridView ItemsSource="{x:Bind Departments}" Grid.Row="1" HorizontalContentAlignment="Stretch" IsItemClickEnabled="True" VerticalContentAlignment="Stretch">
<GridView.ItemContainerStyle>
<Style TargetType="GridViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
</GridView.ItemContainerStyle>
<GridView.ItemTemplate>
<DataTemplate x:DataType="model:Department">
<Grid HorizontalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="250" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="40" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<TextBlock Text="{Binding Name}" Grid.Column="0" />
<TextBlock Text="{Binding Address}" Grid.Column="0" Grid.Row="1" />

<TextBlock Text="{Binding USD.Date}" Grid.Column="1" Grid.RowSpan="2"/>

<TextBlock Text="{Binding USD.Sell}" Grid.Column="2" />
<TextBlock Text="{Binding USD.Buy}" Grid.Column="2" Grid.Row="1"/>
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>

最佳答案

默认情况下,GridView 使用 ItemsWrapGrid作为放置和排列 GridViewItem 的面板,您可以在 Live Visual Tree 中找到它:
enter image description here

虽然您已经为 GridViewItem 设置了 Horizo​​ntalContentAlignmentHorizo​​ntalAlignmentStretch,但是 的大小>GridViewItem 受限于 ItemsWrapGrid

ItemsWrapGrid positions child elements sequentially from left to right or top to bottom in an ItemsControl that shows multiple items. When elements extend beyond the container edge, elements are positioned in the next row or column.

所以在 ItemsWrapGrid 中,项目不会被拉伸(stretch)。

如果您想水平拉伸(stretch) GridViewItem,我们可以使用 ItemsStackPanel 而不是 ItemsWrapGrid,如下所示:

Resources中,添加一个带有ItemsStackPanelItemsPanelTemplate:

<Page.Resources>
<ItemsPanelTemplate x:Key="MyItemsPanelTemplate">
<ItemsStackPanel />
</ItemsPanelTemplate>
</Page.Resources>

然后在 GridView 中使用这个模板:

<GridView HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Stretch"
ItemsPanel="{StaticResource MyItemsPanelTemplate}"
ItemsSource="{x:Bind Banks}"
SelectionMode="None">
...
<GridView Grid.Row="1"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Stretch"
IsItemClickEnabled="True"
ItemsPanel="{StaticResource MyItemsPanelTemplate}"
ItemsSource="{x:Bind Departments}">
...
</GridView>
...
</GridView>

或者我们可以使用 ListView 而不是 GridView 因为 ListView 默认的 ItemsPanelItemsStackPanel

enter image description here

关于c# - UWP 拉伸(stretch) GridView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35427363/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com