簡體   English   中英

另一個內的Scrollviewer

[英]Scrollviewer within another

我不久前就開始使用WPF,並且一直在處理一個ScrollViewer問題,該問題實際上似乎很常見:我有一個ScrollViewer,其內容各不相同,與其他內容一起顯示,整個內容在另一個ScrollViewer中。 整個過程看起來像這樣:

<ScrollViewer HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Auto" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
    <StackPanel Orientation="Vertical">
        <Rectangle Height="50" Width="50" Margin="5" Fill="Orange"/>
        <Rectangle Height="50" Width="50" Margin="5" Fill="Orange"/>
        <ScrollViewer HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Auto">
            <StackPanel Orientation="Vertical">
                <Rectangle Height="50" Width="50" Margin="5" Fill="Blue"/>
                <Rectangle Height="50" Width="50" Margin="5" Fill="Blue"/>
                <Rectangle Height="50" Width="50" Margin="5" Fill="Blue"/>
            </StackPanel>
        </ScrollViewer>
        <Rectangle Height="50" Width="50" Margin="5" Fill="Orange"/>
        <Rectangle Height="50" Width="50" Margin="5" Fill="Orange"/>
    </StackPanel>
</ScrollViewer>

我要的是讓內部ScrollViewer在外部的ScrollViewer之前開始減少其內容,這意味着如果我在一個窗口中擁有所有這些內容並減小窗口的大小,我希望帶有藍色矩形的ScrollViewer在開始之前減少其內容並顯示其滾動條。最外面的一個,包含橙色矩形的那個。

在互聯網上瀏覽了很長一段時間后,我發現了兩個主要建議:第一個建議是避免使用StackPanel-在這個簡化的示例中,我顯然可以使用Grid,但實際上在實際項目中我不確定可以這樣做,因為我放入此ScrollViewer中的內容的大小因我在其中綁定的內容而異。 第二個是我在一個非常相似的問題的答案中發現的:

嵌套滾動區域

起初看起來效果不錯。 產生的xaml變為:

<ScrollViewer HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Auto" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
    <StackPanel Orientation="Vertical">
        <Rectangle Height="50" Width="50" Margin="5" Fill="Orange"/>
        <Rectangle Height="50" Width="50" Margin="5" Fill="Orange"/>
        <local:RestrictDesiredSize MinHeight="60">
            <ScrollViewer HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Auto">
                <StackPanel Orientation="Vertical">
                    <Rectangle Height="50" Width="50" Margin="5" Fill="Blue"/>
                    <Rectangle Height="50" Width="50" Margin="5" Fill="Blue"/>
                    <Rectangle Height="50" Width="50" Margin="5" Fill="Blue"/>
                </StackPanel>
            </ScrollViewer>
        </local:RestrictDesiredSize>
        <Rectangle Height="50" Width="50" Margin="5" Fill="Orange"/>
        <Rectangle Height="50" Width="50" Margin="5" Fill="Orange"/>
    </StackPanel>
</ScrollViewer>

但是現在我遇到了相反的問題:即使我擴大窗口,內部的滾動查看器也始終停留在60的高度,但是我希望它占用盡可能多的空間-如果出現以下情況,則顯示所有藍色矩形並隱藏滾動條可能

有誰知道如何做到這一點?

編輯:

我找到了一個臨時解決方案,該解決方案不是通過在某個地方使用DockPanel而不是StackPanel來實現的,這是不完美的,我想通過避免一起使用stackpanels來解決這個問題真的會更容易嗎? 這是我的最新版本

<ScrollViewer HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Auto" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" VerticalAlignment="Stretch">
    <DockPanel>
        <StackPanel DockPanel.Dock="Top">
            <Rectangle Height="50" Width="50" Margin="5" Fill="Orange"/>
            <Rectangle Height="50" Width="50" Margin="5" Fill="Orange"/>
        </StackPanel>
        <StackPanel DockPanel.Dock="Bottom">
            <Rectangle Height="50" Width="50" Margin="5" Fill="Orange"/>
            <Rectangle Height="50" Width="50" Margin="5" Fill="Orange"/>
        </StackPanel>
        <local:RestrictDesiredSize MinHeight="60">
            <ScrollViewer HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Auto" VerticalContentAlignment="Stretch" VerticalAlignment="Stretch">
                <StackPanel Orientation="Vertical" VerticalAlignment="Stretch">
                    <Rectangle Height="50" Width="50" Margin="5" Fill="Blue"/>
                    <Rectangle Height="50" Width="50" Margin="5" Fill="Blue"/>
                    <Rectangle Height="50" Width="50" Margin="5" Fill="Blue"/>
                </StackPanel>
            </ScrollViewer>
        </local:RestrictDesiredSize>
    </DockPanel>
</ScrollViewer>

現在還不完美,因為部分內容已停靠在底部,但是除非能找到更好的方法,否則我現在將嘗試作為一種變通方法。

我過去通過使用RelativeSource將適當的寬度綁定到父ActualWidth來解決此問題。

請參閱如何將WPF綁定與RelativeSource一起使用?

Snoop對於確定如何在可視樹中查找所需的屬性也非常有用,請參閱我在ReSharper WPF上關於Snoop的教程錯誤:“由於未知的DataContext,無法解析符號“ MyVariable””

暫無
暫無

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

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