具有ScrollBars的WPF XAML ListView [英] WPF XAML ListView with ScrollBars

查看:68
本文介绍了具有ScrollBars的WPF XAML ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个同时显示两个滚动条的ListView.为此,我将ListView放置在如下所示的Grid中的Border内:

I have a ListView which is showing both scroll bars. To achieve that, I places my ListView within a Border located in a Grid like below:

<Window ...
    MinWidth="600" MinHeight="500" Width="800" Height="500"
    Topmost="True"
    WindowStartupLocation="CenterScreen" 
    SizeToContent="Height" WindowStyle="None">

    <Window.Resources>
        <ResourceDictionary>
            ...
        </ResourceDictionary>
    </Window.Resources>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*" MinHeight="60"/>
            <RowDefinition Height="48"/>
        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="10"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>

        <Grid Grid.Row="1">
            <Grid.RowDefinitions>
                <RowDefinition Height="32"/>
                <RowDefinition Height="370"/>  <-- THIS IS HARDCODED TO 370 BUT I NEED IT TO BE RESIZABLE
            </Grid.RowDefinitions>

            <Grid Margin="5,0,0,0">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="4"/>
                    <ColumnDefinition Width="346*"/>
                </Grid.ColumnDefinitions>
                <TextBlock Text="{Binding MyName}" Grid.Column="1"
                       Style="{StaticResource MyNameStyle}" Margin="0,5,0,5" />

            </Grid>

            <Border BorderBrush="{StaticResource MyBrush}" Grid.Row="1" Grid.Column="0" >
                <ListView
                    x:Name="lstMyListView" 
                    Margin="0,0,0,0"
                    BorderBrush="Transparent"
                    ItemsSource="{Binding MyItems}" 
                    SelectedIndex="0"
                    ScrollViewer.CanContentScroll="True"
                    ScrollViewer.VerticalScrollBarVisibility="Auto">
                  ...
                  ...
                </ListView>
              ...
              ...
            </Border>
            ...
            ...
        </Grid>
    </Grid>
</Window>

这都是可调整大小的形式.因此,ListView也应该调整大小,但是只有在用户调整表单大小时才应该这样做.我上面的硬编码值的问题很明显,ListView不会调整大小,而是保持恒定的高度= 370.

This is all in a form that is resizable. Therefore, the ListView should also resize but it should do that only when user resizes the form. The problem with my hard coded value above is obvious, the ListView will not resize but stay at constant height=370.

我知道我可以将其设置为370 *,但在这种情况下,我的列表将调整大小以适合所有项目.我想限制此大小,以便仅在用户调整表单大小时才进行大小调整.因此,如果还有更多项目,则ListView应该显示滚动条,并且随着用户调整窗体的大小,如果将窗体的大小调整为可以容纳ListView中所有项目的高度,则滚动条应该消失.

I know I can set this to 370* but in that case, my List will resize to fit all items. I want to limit this so that resizing only occurs when user resizes the form. So, ListView should show scroll bars if there are more items and as the user resizes form, that scroll bar should go away if form is resized to height that can accommodate all items in ListView.

更新: 我没有尝试将高度硬编码为370,而是尝试将高度设置为,自动和370 .所有这些都将扩展ListView(从而也扩展窗体)以容纳ListView中的所有项目.

UPDATE: Instead of hard coding the height to 370 above, I have tried setting the height to , Auto, and 370. All of these would expand the ListView (and therefore form, too) to accommodate all items in the ListView.

更新2: 更新了XAML以显示整个树结构.

UPDATE 2: Updated XAML to show whole tree structure.

更新3: 根据Rachel的建议,在XAML的上面的注释行中将硬编码370设置为"*",这会产生可调整大小的表单,以便ListView适合其中的所有项目.我添加了显示表单打开时的屏幕截图和显示表单打开时的外观的屏幕截图.如您所见,它会调整高度以适应所有itsm.

UPDATE 3: As per Rachel's suggestion, set hardcoded 370 to "*" in commented line above in XAML and that produces form resized so that the ListView fits all items in it. I added screenshot showing the form as it opens and a screenshot showing how it should look like when it opens. As you can see, it resizes hightwise to accomodate all itesm.

我需要的是表单和ListView保持设置的大小,并且仅在用户调整表单大小时才调整大小.

What I need is that form and ListView stay in their set size and resize only if user resizes the form.

推荐答案

如果我正确理解了您的问题,则表示表单加载的大小过大,因为ListView会增长到全高.如果是这种情况,请从<Window>标记中删除SizeToContent="Height",因为它使窗口的初始高度等于内容的任何高度,其中包括全尺寸的ListView.

If I understand your question correctly, you are saying the form loads as too large of a size because the ListView is growing to it's full height. If that's the case, remove the SizeToContent="Height" from the <Window> tag since it is making the window's initial height be equal to whatever height the content is, which includes the fully sized ListView.

这篇关于具有ScrollBars的WPF XAML ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆