secretgenes February 2016

How to control the height of WPF Application window based on the contents of dynamic Grid

I have created a WPF application that generate tiles in the second row of grid. What i'm trying to achieve is to keep rendering tiles in the second row without showing the vertical scroll bar, until unless the height of WPF application crosses the resolution of user screen.

<Window x:Class="ABC.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="ABC Installation" MinWidth="620" SizeToContent="WidthAndHeight" AllowsTransparency="True" WindowStyle="None" Loaded="MainWindow_loaded" MinHeight="600" >
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="60" />
            <RowDefinition Height="56" />
            <RowDefinition Height="Auto" ScrollViewer.IsDeferredScrollingEnabled="True" />            
            <RowDefinition Height="94"/>
        </Grid.RowDefinitions>
</Grid>

    <ScrollViewer Name="productsOuterScroll" Grid.Row="2" HorizontalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollBarVisibility="Auto" Grid.RowSpan="1" >
        <StackPanel x:Name="FormStackPanel">
        </StackPanel>
    </ScrollViewer>

This code renders all the tiles exceeding the user screen window height without a vertical scroll bar.

Any idea how to do this? Any help would be appreciated.

Answers


Nemanja Banda February 2016

If you want the content to scroll, remove the Height from the RowDefinition of the row that you are adding content to, and put the content inside the ScrollViewer.

Your window might be sizing correctly to the vertical resolution of the screen, but if it's displayed in the center of the screen, it will exceed the height. You can set the startup location to top of the screen with:

WindowStartupLocation="Manual" Top="0"

If the window height is too high, you might want to set maximum height for your window.


secretgenes February 2016

I figured out the mistake. Only two things needed to be done.

  1. remove the height attribute from row definition so it should look like this

    <Window x:Class="ABC.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="ABC Installation" MinWidth="620" SizeToContent="WidthAndHeight" AllowsTransparency="True" WindowStyle="None" Loaded="MainWindow_loaded" MinHeight="600" >
    
    
    <Grid>  
        <Grid.RowDefinitions>
           <RowDefinition Height="60" />
           <RowDefinition Height="56" />
           <RowDefinition  ScrollViewer.IsDeferredScrollingEnabled="True" />
           <RowDefinition Height="94"/>
        </Grid.RowDefinitions>
    
       <ScrollViewer Name="productsOuterScroll" Grid.Row="2" HorizontalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollBarVisibility="Auto" Grid.RowSpan="1" >
          <StackPanel x:Name="FormStackPanel">
          </StackPanel>
       </ScrollViewer>
    </Grid>
    
  2. Setting the max Height dynamically according to the users primary screen height in .cs file

       double userheightscreen = System.Windows.SystemParameters.PrimaryScreenHeight;
       this.MaxHeight = userheightscreen - 100;
    

PS: "-100" is just to leave some space on top and bottom of the screen.

Post Status

Asked in February 2016
Viewed 3,693 times
Voted 8
Answered 2 times

Search




Leave an answer