1

I have a ListBox in my code that resizes it's window to the height of it's contents on creation and doesn't shows it's scroll bar, I want the window to start at it's specified size 301*287. If I resize the window smaller then the content of the ListBox exceeds it's height and the scroll bar appears, which is what I want but I don't want to have to resize the window every time it's created to achieve that. I have tried the answers in this question but none of them seemed to work.

In short the ListBox should be sized to the grid definitions on a 301*287 window at creation but doesn't, instead sizing itself to the height of it's content and making the window's height bigger than it should be.

<Window x:Class="Test.Dialogs.DatatypesDialog"
        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:Test.Dialogs"
        mc:Ignorable="d"
        Title="DatatypesDialog" Width="301" Height="287" Background="Black" SizeToContent="WidthAndHeight" MinWidth="301" MinHeight="287" WindowStartupLocation="CenterOwner">
    <Grid Margin="0" >
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*" />
            <ColumnDefinition Width="1*" />
        </Grid.ColumnDefinitions>
        <ListBox Grid.Column="0" x:Name="lbDTypes" Margin="10,10,5,10" SelectionMode="Single" SelectionChanged="lbDTypes_SelectionChanged"/>
        <Grid Grid.Column="1" Margin="0,8,5,8" >
            <Grid.RowDefinitions>
                <RowDefinition Height="1*" />
                <RowDefinition Height="1*" />
                <RowDefinition Height="1*" />
                <RowDefinition Height="1*" />
                <RowDefinition Height="1*" />
                <RowDefinition Height="1*" />
                <RowDefinition Height="1*" />
                <RowDefinition Height="1*" />
                <RowDefinition Height="1*" />
                <RowDefinition Height="1*" />
            </Grid.RowDefinitions>
            <TextBox Grid.Row="0" x:Name="tbDType" Margin="5,2,5,2" TextWrapping="Wrap" Text="" />
            <Button Grid.Row="1" Margin="5,2,5,2" Content="New" Click="NewDType"   />
            <Button Grid.Row="2" Margin="5,2,5,2" Content="Modify" Click="EditDType"/>
            <Button Grid.Row="3" Margin="5,2,5,2" Content="Remove" Click="RemoveDType"/>
            <Separator Grid.Row="4" Margin="5,2,5,2"  />
            <Button Grid.Row="5" Margin="5,2,5,2" Content="Move Up" Click="MoveUpDType"  />
            <Button Grid.Row="6" Margin="5,2,5,2" Content="Move Down" Click="MoveDownDType" />
            <Separator Grid.Row="7" Margin="5,2,5,2" />
            <Button Grid.Row="8" Margin="5,2,5,2" Content="Save" Click="Save"/>
            <Button Grid.Row="9" Margin="5,2,5,2" Content="Close" Click="Close"/>
        </Grid>
    </Grid>
</Window>
Crimson Bloom
  • 287
  • 2
  • 13

1 Answers1

1

The property SizeToContent="WidthAndHeight" is responsible for this behavior. Remove the property or set it to another value ("Manual" or "Width") and the window should no longer adapt to the listbox.