让窗口滚动

时间:2012-10-10 00:30:26

标签: wpf xaml

我希望窗口根据内容的大小而不是窗口大小滚动。目前,仅当窗口高度小于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>

2 个答案:

答案 0 :(得分:3)

内部网格中没有外部网格和多个堆栈面板。回到基础。网格有一个ScrollViewer和一个StackPanel。

答案 1 :(得分:0)

我发现如果我在ScrollViewer中包装而不是在网格中包含ScrollViewer,我可以在Grid中拥有我想要的任何内容,而Window仍然可以正常滚动。