-1

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

4

2 回答 2

3

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

于 2012-10-10T01:08:44.587 回答
0

我发现如果我将 ScrollViwer 包装在 ScrollViwer 中,而不是将 ScrollViewer 放在 Grid 中,我可以在 Grid 中拥有我想要的任何东西,并且 Window 仍然可以正常滚动。

于 2012-10-11T16:28:02.570 回答