簡體   English   中英

如何在不指定靜態高度或寬度的情況下使ScrollViewer工作?

[英]How to make a ScrollViewer work without specifying a static height or width?

我目前正在使用WPF應用程序的UI,並且我想使用ScrollViewer來顯示可能超出屏幕查看區域的內容。

在過去的兩天里,我已經在互聯網上閱讀了所有內容,據我了解:除非是靜態確定的,否則ScrollViewer並不知道內容/父母的身高。 因此,如果沒有寫下特定的高度(例如,在其下面的StackPanel的情況下),則不允許滾動。

現在,假設我的UI層次結構如下所示:

<Window> <!--No height here-->
    <Grid>
        <StackPanel Orientation="Horizontal"/>
        <ContentControl>
            <UserControl> <!--This user control doesn't have a specific height or width-->
                <Grid>
                    <ScrollViewer>
                        <Grid /> <!--This grid doesn't contain any StackPanels, or containers with dynamic height, and this is content I want to show-->
                    </ScrollViewer>
                    <Grid />
                </Grid>
            </UserControl>
        </ContentControl>
    </Grid>
</Window>

我希望ScrollViewer能夠正確滾動,因為它下面沒有動態容器,但是它沒有,而且看來,為其設置靜態高度或在其上方的UserControl上設置靜態高度可以使其正常工作。

但是由於該應用程序可以在不同的屏幕尺寸上運行,並且所有窗口都可以調整大小,所以我不想編寫靜態尺寸。

解決的辦法是對網格使用分數大小(以某種方式給出scrollviewer的高度),例如:

<Grid>
  <Grid.RowDefinitions>
    <RowDefinition Height="*"/>
    <RowDefinition Height="Auto" /> <!--this gives the second row the space it needs and leaves the scrollviewer with all the remaining space, this will also work if you need to make the second row child fixed-->
  </Grid.RowDefinitions>
  <ScrollViewer Grid.Row="0"/>
  <Grid Grid.Row="1"/>
</Grid>

為了獲得更好的網格體驗,請使用WpfAutoGrid。

暫無
暫無

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

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