XAML 如何使列表框自动添加滚动条而不改变列表框的高度?

gkn4icbw  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(121)

如何让ListBox自动添加一个scrollBar,不改变ListBox的高度,当内容更新时?
我制作了一个窗体,它可以拉伸并允许您在ListBox元素中看到更多行。但是,当我一次向ListBox添加许多项时,ListBox的高度会增加,前景会低于屏幕。如果我拉的形式与我的手滚动条出现,一切看起来校对。
我想实现的窗口行为:
1.如果我们手动拉伸窗口,
1.在添加项目时不会增加列表框的高度,但会立即添加滚动条
开始视图:

添加项目后视图错误:

添加项目后的完美视图:

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?Canvas StackPanel
1.使用不带 Package 器的列表框
1.我尝试了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>
hvvq6cgz

hvvq6cgz1#

解决办法很简单。这个行为给出了窗口属性:SizeToContent="WidthAndHeight"。删除后,滚动条正确显示。谢谢@Clemens指出我的错误。

相关问题