【问题标题】:Where and how should I implement ScrollViewer in my XAML?我应该在哪里以及如何在我的 XAML 中实现 ScrollViewer?
【发布时间】:2010-11-18 13:35:50
【问题描述】:

所以如果(大致)我的 XAML 树是这样的:

<TabControl Name="tab1">
            <TabItem Header="Untitled" Name="tabMain">
                <Canvas Name="canvasTest" DockPanel.Dock="Right">
                <local:VisualsHost Canvas.ZIndex ="99" x:Name="vshMain"></local:VisualsHost>
                    <ListBox Name="lstTiles" DockPanel.Dock="Right" SelectionMode="Extended" PreviewMouseRightButtonDown="grdMain_MouseRightButtonDown" 
             PreviewMouseRightButtonUp="grdMain_MouseRightButtonUp" MouseDown="lstTiles_MouseDown" >
                    <ListBox.Template>
                        <ControlTemplate>
                            <ScrollViewer>
                                <ItemsPresenter />
                            </ScrollViewer>
                        </ControlTemplate>
                    </ListBox.Template>
                        <ListBox.ItemContainerStyle>
                            <Style>
                                <Setter Property="Grid.Row" Value="{Binding RelativeSource={x:Static RelativeSource.Self},
                    Path=Content.Row}"/>
                                <Setter Property="Grid.Column" Value="{Binding RelativeSource={x:Static RelativeSource.Self},
                    Path=Content.Column}"/>
                                <Setter Property="ListBoxItem.Height" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
                                AncestorType={x:Type Window}}, Path=lstboxHeight}" />
                                <Setter Property="ListBoxItem.Width" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
                                AncestorType={x:Type Window}}, Path=lstboxWidth}" />
                                <Setter Property="ListBoxItem.IsHitTestVisible" Value="True" />
                                <Style.Resources>
                                    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Blue" Opacity=".3" />
                                    <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
                                </Style.Resources>
                            </Style>
                        </ListBox.ItemContainerStyle>
                        <ListBox.ItemsPanel>
                            <ItemsPanelTemplate>
                                <Grid ShowGridLines="True" IsItemsHost="True" Background="{DynamicResource LoadedImage}" 
                      Name="grdMain"> 
                            </Grid>
                            </ItemsPanelTemplate>
                        </ListBox.ItemsPanel>
                    </ListBox>
            </Canvas>
        </TabItem>
    </TabControl>

将滚动查看器放在我的列表框周围没有任何作用。如上所示放置 ControlTemplate 也无济于事。我的网格的宽度/高度(如您所见,设置为我的 listboxitem 模板)动态扩展和缩小,但是当它扩展超出窗口大小时,仍然没有滚动条。

【问题讨论】:

    标签: .net wpf wpf-4.0 .net-4.0


    【解决方案1】:

    由于 ListBox 位于 Canvas 内,因此不会随着容器调整大小而调整其大小。 Canvas 本身可以超出其容器的边界。

    ListBox 内置了 ScrollViewer,用于当列表内容超过 ListBox 的最大大小时,但您永远不会超过此大小,因为 ListBox 只会增长,因为它不受 Canvas 的约束。

    您正在使用的 DockPanel 附加属性不会为布局做任何事情。我建议将 Canvas 替换为 Grid 容器,这将限制 Listbox 的大小。

    【讨论】:

    • 好答案。问题是画布。 @Ilya:如果您不理解 Guy 的声明“ListBox 内置了 ScrollViewer”:他的意思是 ListBox 的默认 ControlTemplate 包含一个 ScrollViewer。这意味着不需要设置 ListBox.Template。另外,请注意,如果您真的打算使用 Canvas(通常是个坏主意),您始终可以在 ListBox 上设置显式高度以强制显示 ScrollViewer。如果您希望滚动条一直可见,请查看 ScrollViewer.VerticalScrollBarVisibility 附加属性。
    • 谢谢。修复并将我的画布更改为网格并且效果很好。
    【解决方案2】:

    您是否尝试在 ItemsPanelTemplate 内的 Grid 周围放置一个 ScrollViewer?

        <ScrollViewer>
            <Grid ShowGridLines="True" IsItemsHost="True" Background="{DynamicResource LoadedImage}"  
                      Name="grdMain">
            </Grid>
        </ScrollViewer>
    

    【讨论】:

    • 有什么用?正如盖伊在他的回答中解释的那样,问题出在画布上。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-04
    • 1970-01-01
    • 1970-01-01
    • 2022-06-18
    • 2011-10-18
    相关资源
    最近更新 更多