Wpf 获取要滚动的窗口

Wpf 获取要滚动的窗口,wpf,xaml,Wpf,Xaml,我希望窗口根据内容的大小而不是窗口的大小滚动。当前,仅当窗口高度小于XAML中指定的高度时,才会显示滚动条。这是XAML代码。基于这里的其他线程,我尝试了各种方法,比如在网格中包装ScrollViewer <Window.Resources> <src:ObservableSwitches x:Key="Switches"/> <Style TargetType="{x:Type TreeViewItem}" x:Key="ModuleSty

我希望窗口根据内容的大小而不是窗口的大小滚动。当前,仅当窗口高度小于XAML中指定的高度时,才会显示滚动条。这是XAML代码。基于这里的其他线程,我尝试了各种方法,比如在网格中包装ScrollViewer

<Window.Resources>
    <src:ObservableSwitches x:Key="Switches"/>
        <Style TargetType="{x:Type TreeViewItem}" x:Key="ModuleStyle">
        <Setter Property="Foreground" Value="Blue"/>
        <Setter Property="FontSize" Value="12"/>
    </Style>

    <Style TargetType="{x:Type TreeViewItem}" x:Key="RedModuleStyle" BasedOn="{StaticResource ModuleStyle}">
        <Setter Property="Foreground" Value="Red"/>
    </Style>


</Window.Resources>

<ScrollViewer IsEnabled="True" VerticalScrollBarVisibility="Visible">

    <Grid Margin="0,0,-211.4,-168">
    <StackPanel HorizontalAlignment="Stretch" Name="StackPanel1" VerticalAlignment="Stretch" Width="Auto" Margin="0,0,188.6,114">

        <StackPanel.Resources> 
            <Style TargetType="{x:Type Label}" x:Key="LabelStyle">
                <Setter Property="Foreground" Value="Blue"/>
                <Setter Property="FontSize" Value="12"/>
                <Setter Property="FontWeight" Value="Bold"/>
            </Style>
        </StackPanel.Resources>



            <Label Content="Switch Name:" Name="Label1" Height="25" HorizontalAlignment="Left"/>

            <Label Content="Software Version:" Name="Label2" HorizontalAlignment="Left" />
        <Label Content="Model Number:" Name="Label3" HorizontalAlignment="left"/>
        <Label Content="IP Address:" Name="Label4" HorizontalAlignment="left"></Label>
        <Label Content="Serial Number:" Name="Label5" HorizontalAlignment="Left"></Label>
        <Label Content="Show Tech Taken:" Name="Label6" HorizontalAlignment="left"/>

    </StackPanel>
    <StackPanel Margin="105,0,218,489">
        <StackPanel.Resources>
            <Style TargetType="{x:Type Label}" x:Key="LabelStyle">
                <Setter Property="FontSize" Value="12"/>
                <Setter Property="FontWeight" Value="Bold"/>
            </Style>
        </StackPanel.Resources>
        <Label Content="{Binding switchName}"  Name="SwitchNameLabel" HorizontalAlignment="left" />
        <Label Content="{Binding swVersion}" Name="VersionLabel" HorizontalAlignment="left"/>
        <Label Content="{Binding switchModel}" Name="ModelNumberLabel" HorizontalAlignment="Left"/>
        <Label Content="{Binding IPAddress}" Name="IPAddressLabel" HorizontalAlignment="Left"/>
        <Label Content="{Binding SerialNumber}" Name="SerialLabel" HorizontalAlignment="Left"/>
        <Label Content="{Binding LastDataCaptureDate}" Name="ShowTechLabel" HorizontalAlignment="Left"/>

    </StackPanel>

    <StackPanel Name="ContentPanel" HorizontalAlignment="Left" Height="Auto" Margin="0,185,0,0" VerticalAlignment="Top" Width="auto"
                  ScrollViewer.VerticalScrollBarVisibility="Auto">
        <TreeView Name="ModulesTreeView" Height="auto" Background="GhostWhite" BorderThickness="0" Width="auto"/>
        <TreeView Name="VSANTreeView"  Height="auto" Background="GhostWhite" BorderThickness="0" Width="auto"/>
        <TreeView Name="FCIPTreeView"   MaxHeight="200" Background="GhostWhite" BorderThickness="0" Width="auto" 
                    ScrollViewer.VerticalScrollBarVisibility="Auto"/>
        <TreeView Name="IVRTreeView" Height="Auto" Background="GhostWhite" BorderThickness="0" Width="Auto"
                  ScrollViewer.VerticalScrollBarVisibility="Auto">

            <TreeViewItem Name="IVRTreeViewItem" Header="IVR Topology" Height="Auto" Background="GhostWhite" BorderThickness="0" Width="Auto">

                <TreeViewItem Name="NatEnabled" Header="NAT Enabled" Background="GhostWhite" BorderThickness="0" Width="Auto">
                <Label Name="NATLabel" Background="GhostWhite" BorderThickness="0" Width="Auto"></Label>

                </TreeViewItem>
            <TreeViewItem Name="Status" Header="Status" Background="GhostWhite" BorderThickness="0" Width="Auto">
                <Label Name="StatusLabel" Background="GhostWhite" BorderThickness="0" Width="Auto"></Label>

            </TreeViewItem>
            <TreeViewItem Name="FabricDistributionEnabled" Header="Fabric Distribution" Background="GhostWhite" BorderThickness="0" Width="Auto">
                <Label Name="FabricDistributionLabel" Background="GhostWhite" BorderThickness="0" Width="Auto"></Label>

            </TreeViewItem>
            <TreeViewItem Name="AFID" Header="AFID Listing" Background="GhostWhite" BorderThickness="0" Width="Auto">
                <ListView Name="AFIDListView" >

                </ListView>
            </TreeViewItem>
            </TreeViewItem>
        </TreeView>

    </StackPanel>
</Grid>
</ScrollViewer>



在一个内部网格中没有外部网格和多个堆栈面板。回到最基本的。带有一个ScrollViewer和一个StackPanel的网格

我发现,如果我在ScrollViwer中包装,而不是在网格中使用ScrollViewer,那么我可以在网格中获得我想要的任何内容,并且窗口仍然可以正常滚动

这是wpf吗?不能从你的xaml判断。是的,这是WPF。添加了一个标签。抱歉。滚动是指滚动条显示,还是希望内容有一个固定的高度,因此总是有东西可以滚动?我希望滚动条显示。Rigth现在如果窗口高于XAML中设置的高度,则窗口底部会切断内容,我无法向下滚动到它。我可以使窗户更高,但我不应该这样做。你没有外部网格,内部网格和外部网格中有多个堆叠面板。背对背。带有一个ScrollViewer和一个StackPanel的网格。