Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent a ListView from expanding the window dimension?

I put a ListView in the middle row of a View. The view is contained in a window that has SizeToContent set to WidthAndHeight. The ListView is initially empty, but the underlying ViewModel fills this list view in the process.

The middle Grid.Row height is set to * to fill the available size of the window. When the ListView receives new items, it will at some point expand the window size instead of displaying the ScrollViewer in the ListView. How can I prevent this behavior to have SizeToContent set to WidthAndHeight, the Grid.Row height to * but not have the ListView expand the window dimensions?

Here's the code for the window (the Workspace property will contain the ViewModel):

<Window x:Class="Views.ContainerWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="{Binding Title}"
        SizeToContent="WidthAndHeight">
   <ContentControl Content="{Binding Workspace}"/>
</Window>

The View for the provided ViewModel looks like this:

<UserControl x:Class="Views.SomeView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             MinHeight="450">
   <Grid>
      <Grid.RowDefinitions>
         <RowDefinition Height="Auto"/>
         <RowDefinition Height="*"/>
         <RowDefinition Height="Auto"/>
      </Grid.RowDefinitions>
      <TextBlock Grid.Row="0" 
                 TextWrapping="Wrap" 
                 Margin="5"
                 Text="Some description text"/>
      <ListView Grid.Row="1"
                ItemsSource="{Binding ItemsList}" 
                Margin="5">
         <ListView.View>
            <GridView>
               ...
            </GridView>
         </ListView.View>
      </ListView>
      <Button Grid.Row="2"
              HorizontalAlignment="Right"
              Command" Value="{Binding CloseCommand}"/>
   </Grid>
</UserControl>
like image 894
Holger Adam Avatar asked Sep 12 '10 12:09

Holger Adam


1 Answers

Would turning off the window autosizing after it has loaded work for you?

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        this.SizeToContent = SizeToContent.Manual;
    }
like image 184
Fredrik Karlsson Peraldi Avatar answered Sep 22 '22 18:09

Fredrik Karlsson Peraldi