none
如何控制listview与外层grid的大小一致 RRS feed

  • 问题

  • 我设计了一个window,目的是2个数据表的展示,如下

        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <CheckBox Content="只显示不同项" HorizontalAlignment="Right" Grid.Column="0"  Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked"/>
            <StackPanel Grid.Row="1">
                <Label Content="生产库"/>
                <ListView Name="sqlSelect_Pro_ListView" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Auto">
                    <ListView.View>
                        <GridView x:Name="gridView1"/>
                    </ListView.View>
                </ListView>
            </StackPanel>
            <StackPanel Grid.Row="2">
                <Label Content="历史库"/>
                <ListView Name="sqlSelect_History_ListView">
                    <ListView.View>
                        <GridView x:Name="gridView2"/>
                    </ListView.View>
                </ListView>
            </StackPanel>
        </Grid>

    我查询2个表以后的输出效果如下

    我发现这两个表的数据远远不这一些。因为ScrollView是设定默认为Auto的,所以我认为是listview的长度并不只有显示出来的这点,所以没有显示ScrollView。请问如何控制listview的长度和显示出来的一样。

    2014年6月10日 5:29

答案

  • 你好,

    你的代码中主要问题是在于把ListView放在了一个StackPanel里面,这个容器不会限制ListView的高度,导致垂直滚动条不会自动出现。

    解决方法很简单吗,使用Grid即可,以下为我修改的XAML:

    <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <CheckBox Content="只显示不同项" HorizontalAlignment="Right" Grid.Column="0" Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked"/>
            <Grid Grid.Row="1">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition />
                </Grid.RowDefinitions>
                <Label Content="生产库"/>
                <ListView Grid.Row="1" Name="sqlSelect_Pro_ListView" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Auto">
                    <ListView.View>
                        <GridView x:Name="gridView1"/>
                    </ListView.View>
                </ListView>
            </Grid>
            <Grid Grid.Row="2">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition />
                </Grid.RowDefinitions>
                <Label Content="历史库"/>
                <ListView Grid.Row="1" Name="sqlSelect_History_ListView" >
                    <ListView.View>
                        <GridView x:Name="gridView2"/>
                    </ListView.View>
                </ListView>
            </Grid>
        </Grid>

    截图:


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年6月11日 5:39
    版主

全部回复

  • 你好,

    你的代码中主要问题是在于把ListView放在了一个StackPanel里面,这个容器不会限制ListView的高度,导致垂直滚动条不会自动出现。

    解决方法很简单吗,使用Grid即可,以下为我修改的XAML:

    <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <CheckBox Content="只显示不同项" HorizontalAlignment="Right" Grid.Column="0" Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked"/>
            <Grid Grid.Row="1">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition />
                </Grid.RowDefinitions>
                <Label Content="生产库"/>
                <ListView Grid.Row="1" Name="sqlSelect_Pro_ListView" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Auto">
                    <ListView.View>
                        <GridView x:Name="gridView1"/>
                    </ListView.View>
                </ListView>
            </Grid>
            <Grid Grid.Row="2">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition />
                </Grid.RowDefinitions>
                <Label Content="历史库"/>
                <ListView Grid.Row="1" Name="sqlSelect_History_ListView" >
                    <ListView.View>
                        <GridView x:Name="gridView2"/>
                    </ListView.View>
                </ListView>
            </Grid>
        </Grid>

    截图:


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年6月11日 5:39
    版主
  • 谢谢你的答复,问题解决了

    2014年6月12日 10:09