从父级继承的 GridView 宽度 [英] GridView width inherited from parent
问题描述
我有一个固定宽度的 StackPanel
.在那个 StackPanel
里面我有一个 GridView
,它的 Width
应该被限制在它的父宽度(就像 Width="*" 一样).>
我的示例 XAML:
<TextBox Width="50" Margin="0" Height="50" Background="Blue"></TextBox><网格视图><GridView.ItemsPanel><ItemsPanelTemplate><WrapGrid Orientation="Horizontal" FlowDirection="LeftToRight"/></ItemsPanelTemplate></GridView.ItemsPanel><GridView.Items><TextBox Width="50" Margin="0" Height="50" Background="Green"></TextBox><TextBox Width="50" Margin="0" Height="50" Background="Green"></TextBox><TextBox Width="50" Margin="0" Height="50" Background="Green"></TextBox><TextBox Width="50" Margin="0" Height="50" Background="Green"></TextBox><TextBox Width="50" Margin="0" Height="50" Background="Green"></TextBox></GridView.Items></GridView></StackPanel>
在这个例子中,GridView
的宽度比父级宽,所以它的一些项目没有显示(没有包装).当我将 GridView
宽度设置为固定值时,项目被包装,但我不能在我的项目中使用固定值.
在这种情况下,使用 Grid
而不是 StackPanel
更有好处.下面的代码将达到预期的效果(GridView
将占用 TextBox
旁边任何未使用的空间).
<Grid.ColumnDefinitions><ColumnDefinition Width="50"/><ColumnDefinition Width="*"/></Grid.ColumnDefinitions><TextBox Grid.Column="0" Height="50" Background="Blue"></TextBox><GridView Grid.Column="1"><GridView.ItemsPanel><ItemsPanelTemplate><WrapGrid Orientation="Horizontal" FlowDirection="LeftToRight"/></ItemsPanelTemplate></GridView.ItemsPanel><GridView.Items><TextBox Width="50" Margin="0" Height="50" Background="Green"/><TextBox Width="50" Margin="0" Height="50" Background="Green"/><TextBox Width="50" Margin="0" Height="50" Background="Green"/><TextBox Width="50" Margin="0" Height="50" Background="Green"/><TextBox Width="50" Margin="0" Height="50" Background="Green"/></GridView.Items></GridView></网格>
I have a StackPanel
with a fixed width. Inside that StackPanel
I have a GridView
, which Width
should be limited to its parent width (smth like Width="*").
My sample XAML:
<StackPanel Orientation="Horizontal" Width="300" Height="300">
<TextBox Width="50" Margin="0" Height="50" Background="Blue"></TextBox>
<GridView >
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<WrapGrid Orientation="Horizontal" FlowDirection="LeftToRight" />
</ItemsPanelTemplate>
</GridView.ItemsPanel>
<GridView.Items>
<TextBox Width="50" Margin="0" Height="50" Background="Green"></TextBox>
<TextBox Width="50" Margin="0" Height="50" Background="Green"></TextBox>
<TextBox Width="50" Margin="0" Height="50" Background="Green"></TextBox>
<TextBox Width="50" Margin="0" Height="50" Background="Green"></TextBox>
<TextBox Width="50" Margin="0" Height="50" Background="Green"></TextBox>
</GridView.Items>
</GridView>
</StackPanel>
In this example the GridView
width is wider than the parent, so some of its items are not displayed (not wrapped). When I set GridView
width to a fixed value items are wrapped, but I can't use fixed value in my project.
In this scenario it's more beneficial to have a Grid
rather than a StackPanel
. The code below will achieve the desired effect (the GridView
will take up any unused space next to the TextBox
).
<Grid Width="300" Height="300">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBox Grid.Column="0" Height="50" Background="Blue"></TextBox>
<GridView Grid.Column="1">
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<WrapGrid Orientation="Horizontal" FlowDirection="LeftToRight" />
</ItemsPanelTemplate>
</GridView.ItemsPanel>
<GridView.Items>
<TextBox Width="50" Margin="0" Height="50" Background="Green"/>
<TextBox Width="50" Margin="0" Height="50" Background="Green"/>
<TextBox Width="50" Margin="0" Height="50" Background="Green"/>
<TextBox Width="50" Margin="0" Height="50" Background="Green"/>
<TextBox Width="50" Margin="0" Height="50" Background="Green"/>
</GridView.Items>
</GridView>
</Grid>
这篇关于从父级继承的 GridView 宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!