如何让ListBox自动添加ScrollBar并且不改变Listbox的高度?

问题描述 投票:0回答:1

如何让ListBox在内容更新时自动添加滚动条,而不改变Listbox的高度?

我制作了一个可拉伸的表单,允许您在 ListBox 元素中看到更多行。但是,当我一次向列表框添加许多项目时,列表框的高度会增加,前景会低于屏幕。如果我用手拉动表格,就会出现滚动条,所有内容看起来都像是校对的。

我想实现窗口行为:

  1. 如果我们手动拉伸窗口,列表框也会拉伸
  2. 添加项目时不会增加列表框的高度,但会立即添加滚动条

开始视图:

添加项目后视图错误:

添加项目后的完美视图:

xaml代码:

        <DockPanel Grid.ColumnSpan="2"
                   Grid.Row="1"
                   MinHeight="80"
                   LastChildFill="True">
            <ListBox x:Name="ListFolders"
                     HorizontalAlignment="Stretch"
                     VerticalAlignment="Stretch"
                     Margin="8 0"
                     ScrollViewer.IsDeferredScrollingEnabled="True"
                     ItemsSource="{Binding SelectedDirectories}"
                     SelectedItem="{Binding SelectedFolder}"
                     MouseDoubleClick="ListBox_MouseDoubleClick"
                     KeyDown="ListFolders_KeyDown">
            </ListBox>

        </DockPanel>
  1. 将ListBox嵌入到DockPanel等不同类型的元素中?画布堆栈面板
  2. 使用不带包装器的列表框
  3. 尝试了ScrollView设置

完整代码:

<Window x:Class="B.Revit.BatchUpgrader.Addin.Views.BatchUpgraderWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:B.Revit.BatchUpgrader.Addin.Views"
    mc:Ignorable="d"
    Title="Batch Upgrader 1.0"
    SizeToContent="WidthAndHeight"
    ResizeMode="CanResize"
    WindowStartupLocation="CenterScreen"
    ShowInTaskbar="False"
    Topmost="True">

<Window.Resources>
    <Style TargetType="Button">
        <Setter Property="Height" Value="32"/>
        <Setter Property="Background" Value="White"/>
        <Setter Property="BorderThickness" Value="2"/>
        <Setter Property="BorderBrush" Value="DarkSeaGreen"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border CornerRadius="5" BorderThickness="1" BorderBrush="#FF9DA57F" Background="#FFF4F7E9">
                        <ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <StackPanel Grid.Row="0"
                Grid.ColumnSpan="2"
                Margin="4">

        <GroupBox Header="File Types"
                  Margin="4">
            <StackPanel>
                <CheckBox Content="Project files *.rvt"
                          Margin="4"
                          IsChecked="{Binding IsProjectUpdate}" />
                <CheckBox Content="Family files *.rfa"
                          Margin="4"
                          IsChecked="{Binding IsFamilyUpdate}" />
                <CheckBox Content="Template files *.rte, *.rft"
                          Margin="4"
                          IsChecked="{Binding IsTemplateUpdate}" />
            </StackPanel>
        </GroupBox>

        <GroupBox Header="Saving settings"
                  Margin="4">
            <StackPanel>
                <RadioButton GroupName="SavingSettings"
                             Content="Add a suffix to the file name"
                             Margin="4"
                             IsChecked="{Binding IsAddSuffixSave}" />
                <RadioButton GroupName="SavingSettings" 
                             Content="Save the updated files in a subfolder"
                             Margin="4"
                             IsChecked="{Binding IsSubFolderSave}" />
                <RadioButton GroupName="SavingSettings"
                             Content="Overwrite existing files"
                             Margin="4"
                             IsChecked="{Binding IsOverwriteSave}" />
            </StackPanel>
        </GroupBox>

        <GroupBox Header="Additional settings"
                  Margin="4">
            <StackPanel>
                <CheckBox Content="Delete backup files after update"
                          Margin="4"
                          IsChecked="{Binding IsDeleteBackup}"
                          IsEnabled="{Binding IsDeleteBackupEnable}"/>
                <CheckBox Content="Update families in subfolders"
                          Margin="4"
                          IsChecked="{Binding IsIncludeSubFolders}" />
                <CheckBox Content="Update by ignoring errors"
                          Margin="4"
                          IsChecked="{Binding IsIgnoringErrors}" />
            </StackPanel>
        </GroupBox>

        <Button Content="Select folder with Revit files"
                Height="32"
                Margin="4"
                Command="{Binding SelectFolderButton}">
        </Button>
    </StackPanel>
        <ListBox x:Name="ListFolders"
                 Grid.Row="1"
                 Grid.Row="1"
                 MinHeight="80"
                 Margin="8 0"
                 ItemsSource="{Binding SelectedDirectories}"
                 SelectedItem="{Binding SelectedFolder}"
                 MouseDoubleClick="ListBox_MouseDoubleClick"
                 KeyDown="ListFolders_KeyDown">
        </ListBox>
    <Button Grid.Row="2"
            Grid.Column="0"
            Content="Upgrade"
            Height="32"
            MinWidth="160"
            Margin="8"
            Command="{Binding RunBatchUpgraderButton}" />
    <Button Grid.Row="2"
            Grid.Column="1"
            Content="Cancel"
            Height="32"
            MinWidth="160"
            Margin="8"
            Command="{Binding CancelBatchUpgraderButton}" />
</Grid>
wpf xaml mvvm uiscrollview listboxitem
1个回答
0
投票

事实证明,解决方案非常简单。此行为赋予了窗口属性:

SizeToContent="WidthAndHeight"
。删除后,滚动条就可以正确显示了。谢谢@Clemens 指出我的错误。

© www.soinside.com 2019 - 2024. All rights reserved.