1

My WPF application has a window that contains a ListBox. For some reason that I don't understand, it started growing on screen today after items are inserted into it. I want its height to stay fixed and I don't want it to grow every time an item is inserted.

Here's the XAML for the window:

<Viewbox Stretch="Uniform">
    <Grid Background="{DynamicResource WindowBackground}"
          Name="LayoutRoot">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <Grid Background="{DynamicResource AlarmTitleBackground}"
              Grid.Row="0"
              MouseLeftButtonDown="LeftMouseButtonDown">
            . . .
        </Grid>

        <Rectangle Fill="{DynamicResource AlarmTitleBackground}"
                   Grid.Row="1"
                   Height="4" />

        <Grid Background="{DynamicResource ControlBackground}"
              Grid.Row="2">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>

            <Grid FocusManager.IsFocusScope="True"
                  Grid.Column="0"
                  Grid.Row="0"
                  Name="PendingAlarmScope">
                <ListBox Background="{DynamicResource TextBackground}"
                         HorizontalContentAlignment="Center"
                         IsSynchronizedWithCurrentItem="True"
                         Margin="5"
                         MinWidth="185"
                         VerticalAlignment="Stretch"
                         Name="AlarmsList"
                         SelectionChanged="AlarmsList_SelectionChanged" />
            </Grid>
            . . .
        </Grid>
    </Grid>
</Viewbox>

I found a post in a blog about a TextBox that kept growing as characters were typed. The author indicated that the TextBox was in a ScrollViewer with the HorizontalScrollBarVisibility property set to "Auto". They changed it to "Disabled" and this fixed it for them. I tried adding ScrollViewer.VerticalScrollBarVisiblility="Disabled" to the xaml but this didn't work. I also tried binding theListBox's MaxHeight property to theActualHeight` of another control that's the same exact height as I want & that didn't work, either.

How do I fix the Height of the ListBox without setting it? I want the ListBox to always fill the Grid cell it's in, and the window to grow and rescale itself for different screen resolutions.

Tony Vitabile
  • 8,298
  • 15
  • 67
  • 123
  • can you try to use the hack provided here? http://stackoverflow.com/questions/386039/wpf-textbox-and-scroll-behavior – dnr3 Mar 04 '14 at 03:41
  • That didn't work, but I believe it's because of the `ViewBox`. See my comment to Blam's answer. – Tony Vitabile Mar 04 '14 at 14:11
  • 1
    Does this answer your question? [How to prevent a ListView from expanding the window dimension?](https://stackoverflow.com/questions/3694883/how-to-prevent-a-listview-from-expanding-the-window-dimension) – Mike Nakis Jul 23 '21 at 20:07
  • @MikeNakis: Thanks very much for that and it probably would fix the problem. Unfortunately, I've been out of that job since 2016 and the question is no longer relevant to me. However, it will be good to know if I'm ever in that situation again. – Tony Vitabile Jul 29 '21 at 20:18

2 Answers2

2
<Grid Background="{DynamicResource ControlBackground}"
              Grid.Row="2">
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>

for grins would you try dropping the view box and some other stuff

<Grid Background="{DynamicResource WindowBackground}"
          Name="LayoutRoot">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <Rectangle Fill="{DynamicResource AlarmTitleBackground}"
                   Grid.Row="1"
                   Height="4" />


                <ListBox Grid.Row="2" Background="{DynamicResource TextBackground}"
                         HorizontalContentAlignment="Center"
                         IsSynchronizedWithCurrentItem="True"
                         Margin="5"
                         MinWidth="185"
                         VerticalAlignment="Stretch"
                         Name="AlarmsList"
                         SelectionChanged="AlarmsList_SelectionChanged" />

    </Grid>
paparazzo
  • 44,497
  • 23
  • 105
  • 176
  • Thanks, but that didn't help. No change in behavior. – Tony Vitabile Mar 03 '14 at 21:44
  • I dropped the `ViewBox` only and the `ListBox` stopped growing. However, the `ViewBox` is needed to make the control look right at different resolutions. I understand why the `ListBox` is growing when it's in the `ViewBox` -- the `ViewBox` makes it think it has infinite room. I need to get the effect on the layout without the `ListBox` growing as things are inserted into it. – Tony Vitabile Mar 04 '14 at 14:10
  • So the problem is the ViewBox and not even worth a +1. Did you try ViewBox(es) in the Grid? – paparazzo Mar 04 '14 at 15:21
  • Dude, no need to get snippy. I gave you a +1. I'm still trying to figure this out & sorry if I didn't do it fast enough for you. – Tony Vitabile Mar 04 '14 at 15:35
2

After many hours of trying numerous things, I finally bit the bullet & set the Height of the ListBox to a value that I found using Snoop before it started growing. This stopped it from growing every time a new item was inserted. It was the only thing I could find to do that worked. A very frustrating day.

Tony Vitabile
  • 8,298
  • 15
  • 67
  • 123
  • I had a similar problem with a ListBox inside of Grids and DockPanels (there was no explicit ViewBox). The listbox was initially being sized to fit with other controls (as expected) but would grow when items were added (undesired) even when `ScrollViewer.VerticalScrollBarVisibility="Visible"` and `VerticalAlignment="Stretch"` were both set on the ListBox :( We also ended up being frustrated and having to hardcode the height. – Ben Jan 06 '15 at 16:19