【问题标题】:WPF TreeView not showing scrollbars when placed inside another ScrollViewer放置在另一个 ScrollViewer 中时 WPF TreeView 不显示滚动条
【发布时间】:2014-01-09 16:53:34
【问题描述】:

我有一个问题,当 TreeView 放置在具有 ScrollViewer 的容器中时,TreeView 的滚动条不起作用,而是调整了容器的 ScrollViewer 内容的大小,以便所有 TreeView 项目都可见。

在 WinForms 中我可以设置容器的最小内容宽度和高度,但是如何在 WPF 中实现呢?

以下是 XAML 示例:

<Window x:Class="TestWpfTreeViewInsideScrollViewer.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <ScrollViewer Name="scroll1" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" >
        <Grid MinHeight="230" MinWidth="200" Grid.IsSharedSizeScope="True">
            <Button Content="Button" Width="74" Height="52" Margin="10,24,0,0" HorizontalAlignment="Left" VerticalAlignment="Top"/>
            <Button Content="Button" Height="52" Margin="89,24,10,0" VerticalAlignment="Top"/>
            <Button Content="Button" Width="74" Height="52" Margin="10,81,0,0" HorizontalAlignment="Left" VerticalAlignment="Top"/>

            <TreeView Margin="10,138,10,55">
                <TreeViewItem IsExpanded="True" Header="This is a long text but the treeview is not scrolling">
                    Instead the TreeView is expanding its size to fit all these nodes inside it, i want that the upper scroller take in only when grid minwidth 200 is reached, not before
                    <TreeViewItem IsExpanded="True" Header="This is a long text but the treeview is not scrolling">This is a long text but the treeview is not scrolling
                    </TreeViewItem>
                </TreeViewItem>
                <TreeViewItem IsExpanded="True" Header="This is a long text but the treeview is not scrolling">This is a long text but the treeview is not scrolling</TreeViewItem>
                <TreeViewItem IsExpanded="True" Header="This is a long text but the treeview is not scrolling">This is a long text but the treeview is not scrolling</TreeViewItem>
            </TreeView>

            <Button Content="Button" Margin="10,0,10,10" Height="40" VerticalAlignment="Bottom"/>
        </Grid>
    </ScrollViewer>

</Grid>

【问题讨论】:

    标签: wpf treeview scrollviewer


    【解决方案1】:

    Scrollviewer 的内容有一个未定义的空间。

    这意味着 TreeView 以不需要显示其 ScrollViewer 的大小显示。

    至少应该定义ScrollViewer里面TreeView的Width和Height。

    更新:

    如果要让 Treeview Width 动态调整大小,则需要使用数据绑定。

    <TreeView Margin="10,138,10,55" Width="{Binding ElementName=scroll1, Path=ActualWidth}">
    

    如果 ScrollViewer 不满足 Treeview Width,也可以将 Width 绑定到其他控件。

    我希望这会有所帮助。

    【讨论】:

    • 是的,但是如果我为网格设置高度和宽度,当我调整窗口大小时它不会调整大小,如何在 ScrollViewer 中设置网格,以便它从外部网格?
    • 也许你需要使用数据绑定来设置 Treeview 的宽度 这可能不正确,但我希望这会有所帮助。 Width="{Binding ElementName=scroll1, Path=ActualWidth}"
    • 嘿,实际宽度绑定有效!谢谢!也许您想完善您的答案,以便我批准!
    • 我使用的解决方法是绑定其他控件。如果没有适合我需要的控件,那么我将创建额外的不可见控件。您可以将前 2 个按钮放在 Grid 中,并将 TreeView 的 with 绑定到 Grid。这假定网格左右边距与 TreeView 的相同。否则,它可能不会给出一个好的结果。
    猜你喜欢
    • 2023-03-20
    • 1970-01-01
    • 2012-08-31
    • 2010-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多