GridView宽度继承自父级

时间:2013-05-08 10:45:46

标签: c# wpf xaml windows-8 windows-runtime

我有一个固定宽度的StackPanel。在StackPanel里面我有一个GridViewWidth应该限制在其父宽度(smth,如Width =“*”)。

我的示例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>

在此示例中,GridView宽度比父级宽,因此某些项目不会显示(未包装)。当我将GridView宽度设置为固定值时,项目被包装,但我不能在我的项目中使用固定值。

2 个答案:

答案 0 :(得分:1)

在这种情况下,拥有Grid而不是StackPanel会更有利。以下代码将达到预期效果(GridView将占用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>

答案 1 :(得分:0)

您可以尝试启用horizo​​ntalscrollmode并将horizo​​ntalscrollbarvisibility设置为true。但数据不会被包裹。