簡體   English   中英

如何基於動態網格的內容控制WPF應用程序窗口的高度

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

我創建了一個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>

此代碼將呈現超出用戶屏幕窗口高度的所有圖塊,而沒有垂直滾動條。

任何想法如何做到這一點? 任何幫助,將不勝感激。

如果要滾動內容,請從要RowDefinition添加內容的行的RowDefinition中刪除Height ,然后將內容放入ScrollViewer中

您的窗口大小可能會正確調整為屏幕的垂直分辨率,但是如果顯示在屏幕中央,則它將超過高度。 您可以使用以下方法將啟動位置設置在屏幕頂部:

WindowStartupLocation="Manual" Top="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; 

PS:“-100”只是在屏幕頂部和底部留出一些空間。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM