【问题标题】:GridView width inherited from parent从父级继承的 GridView 宽度
【发布时间】:2013-05-08 10:45:46
【问题描述】:

我有一个固定宽度的StackPanel。在StackPanel 里面我有一个GridViewWidth 应该被限制在它的父宽度(smth like 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 宽度设置为固定值时,项目被包装,但我不能在我的项目中使用固定值。

【问题讨论】:

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


【解决方案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>

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2015-03-25
      • 2018-11-05
      • 1970-01-01
      • 2018-08-07
      • 1970-01-01
      • 2013-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多