How to make ListBox automatically add a ScrollBar and not change the height of the Listbox?

71 Views Asked by At

How to make ListBox automatically add a scrollBar, not change the height of the Listbox, when the content is updated?

I made a form that stretches and allows you to see more rows in the ListBox element. But when I add many items to the ListBox at once, the height of the ListBox increases and the foreground goes below the screen. If I pull the form with my hand a scrollbar appears and everything looks proofreader.

I want to achieve the window behavior that:

  1. Will stretch the ListBox if we stretch the window by hand
  2. Will not increase the height of the Listbox when adding items, but will immediately add a srollbar

start view:
enter image description here

wrong view after adding items:
enter image description here

perfect view after adding items:
enter image description here

xaml code:

        <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. embed ListBox in different types of elements such as DockPanel? Canvas StackPanel
  2. use ListBox without wrappers
  3. tried the ScrollView settings

full code:

<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>
1

There are 1 best solutions below

2
Mikita Yankouski On

The solution turned out to be simple enough. This behavior gave the window property: SizeToContent="WidthAndHeight". After removing it, the scrollbar appears correctly. Thanks, @Clemens for pointing out my error.