如何根据动态网格

时间:2016-02-08 10:02:15

标签: c# wpf visual-studio-2012

我创建了一个WPF应用程序,用于在第二行网格中生成切片。我想要实现的是在不显示垂直滚动条的情况下继续渲染第二行中的切片,直到除非WPF应用程序的高度超过用户屏幕的分辨率。

<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>

此代码在没有垂直滚动条的情况下呈现超出用户屏幕窗口高度的所有切片。

知道怎么做吗?任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

如果您希望滚动内容,请从要添加内容的行的Height中删除RowDefinition,并将内容放在ScrollViewer内。

您的窗口可能正确调整到屏幕的垂直分辨率,但如果它显示在屏幕中央,则会超出高度。您可以使用以下命令将启动位置设置为屏幕顶部:

WindowStartupLocation="Manual" Top="0"

如果窗口高度太高,您可能需要为窗口设置最大高度。

答案 1 :(得分:0)

我弄明白了这个错误。只需要完成两件事。

  1. 从行定义中删除height属性,使其看起来像这样

    <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. 根据.cs文件中的用户主屏幕高度动态设置最大高度

       double userheightscreen = System.Windows.SystemParameters.PrimaryScreenHeight;
       this.MaxHeight = userheightscreen - 100;
    
  3. PS:“-100”只是在屏幕的顶部和底部留下一些空间。

相关问题