0

I have a grid that contains 2 child controls. I have a simple stack panel and a ListBox that will reside in the grid:

<Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="5" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <ListBox Name="lstGroups" Grid.Row="0" />
        <StackPanel Grid.Row="2" />
<Grid>

The problem is that my ListBox renders past the viewable screen area that's assigned to the grid. How can I ensure that my ListBox takes up the available space but that it doesn't render past the second row where I need a vertical scrollbar to see everything?

MickeySixx
  • 195
  • 1
  • 2
  • 15
  • Does the ListBox have a minimum size? The style may be setting one. Otherwise your code should be preventing the ListBox from being any larger than Grid.Height - StackPanel.Height - 5. I'm assuming you meant to put the StackPanel in Row 2 not Row 1. – NtscCobalt Aug 06 '12 at 19:33
  • No, the layout above is correct. There are no Min/Max's - just the xaml you see here. What I'd like is to have the listBox constained to the viewing area of row 0 -- the problem is that it extendeds past the viewable area meaning I have to add a scrollbar and I don't want any scrollbar. – MickeySixx Aug 06 '12 at 19:40
  • You have three row definitions - what are they each for? I'm guessing "*" is for the ListBox. Did you mean for the StackPanel to be in row 2 and not row 1? – ChimeraObscura Aug 06 '12 at 21:57
  • yes sorry but listBox is in Row 0, Horizontal stackPanel in row 2 – MickeySixx Aug 06 '12 at 23:39

1 Answers1

0

You should probably use a DockPanel for this. Also you could programmatically set the height of your Listbox, but that's not a very clean way to do it.

<Window x:Class="MobilWPF.Windows.testWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="testWindow" Height="300" Width="300">
    <DockPanel>
        <StackPanel DockPanel.Dock="Bottom" >
            <TextBlock Text="blah"/>
        </StackPanel>
        <ListBox Name="lstGroups"  />
    </DockPanel>
</Window>


namespace MobilWPF.Windows
{
    /// <summary>
    /// Interaction logic for testWindow.xaml
    /// </summary>
    public partial class testWindow : Window
    {
        public testWindow()
        {
            InitializeComponent();

            for (int i = 0; i < 200; i++)
            {
                lstGroups.Items.Add(i.ToString()); 
            }
        }
    }
}
patrick
  • 16,091
  • 29
  • 100
  • 164
  • I get the same behavior with DockPanel – MickeySixx Aug 06 '12 at 19:58
  • 1
    @MickeySixx, I updated my answer and tested it. The only difference is that my window has a fixed Height and Width. So I guess one of the controls above what you posted has an infinite height. – patrick Aug 06 '12 at 20:05