我确实有一个关于在滚动查看器中使用 ListView 进行布局的问题。一旦 ListView 位于滚动查看器中,它就会使用它的最大尺寸并且不会自行滚动,因为滚动查看器会为其内部的控件提供无限量的空间。 问题是,长列表下方的控件只有在用户向下滚动时才可见,我想让 ListView 仅使用必要的空间并使用滚动条本身。图片确实比文字说明了更多信息(图片链接也确实说明了很多,因为我的声誉还没有达到 10.. Edit2:好吧,我只能使用一个链接,所以我将所有图片复制到一张)。 如果列表不长,一切正常:
图一:
现在,如果列表较长,下面的控件会向下移动到看不见的地方:
图2:见图1中的链接
我现在想要发生的是:
图3:见图1中的链接
这本身并不是真正的问题,因为我们可以将所有内容都放在停靠面板中,并将下方的控件停靠到 Dock.Below 和 Top to Top,并让 ListView 用“lastchildfill”填充中心。现在是真正的问题。如果窗口变小怎么办?然后首先 ListView 消失,然后其他所有内容都没有滚动条滚动到底部的控件。
图 4:见图 1 中的链接
我正在寻找的理想解决方案是在窗口(或根滚动查看器)上有滚动条,这将使我们能够像这样滚动到窗口的每个部分并且只有外部滚动条一旦一切都达到最小尺寸就可见。
图5:见图1中的链接
有什么想法吗?图片太多?这里有一些 xaml 供大家尝试使其工作(这只是一个快速示例窗口...)
<Window x:Class="WpfTest1.ScrollTestWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="ScrollTestWindow" Height="400" Width="700">
<ScrollViewer >
<DockPanel LastChildFill="True" ScrollViewer.CanContentScroll="True" ScrollViewer.HorizontalScrollBarVisibility="Visible">
<Grid DockPanel.Dock="Top">
<TextBlock Text="Example controls above listview" Background="LightGray" FontSize="30"></TextBlock>
</Grid>
<Grid DockPanel.Dock="Bottom">
<TextBlock Text="Example controls below listview" Background="LightGray" FontSize="30"></TextBlock>
</Grid>
<ListView FontSize="30">
<ListView.View>
<GridView>
<GridViewColumn Width="190" Header="Date" />
<GridViewColumn Width="200" Header="Day Of Week" DisplayMemberBinding="{Binding DayOfWeek}" />
<GridViewColumn Width="120" Header="Year" DisplayMemberBinding="{Binding Year}" />
</GridView>
</ListView.View>
<sys:DateTime>1/1/1</sys:DateTime>
<sys:DateTime>1/1/1</sys:DateTime>
<sys:DateTime>1/1/1</sys:DateTime>
<sys:DateTime>1/1/1</sys:DateTime>
<sys:DateTime>1/1/1</sys:DateTime>
<sys:DateTime>1/1/1</sys:DateTime>
<sys:DateTime>1/1/1</sys:DateTime>
<sys:DateTime>1/1/1</sys:DateTime>
</ListView>
</DockPanel>
</ScrollViewer>
最佳答案
好的。所以我遇到了同样的问题,但现在已经设法解决了!
我的项目看起来与您的略有不同,但我认为它应该适合您。我提出的解决方案也有一些局限性。例如,只有当您只添加一个 ListView 时它才会起作用!在您的示例中,您只有一个,所以那里不会有问题。但是对于可能需要更多 ListViews 的任何人,您必须添加更多功能来决定 View 的大小以及放置它们的位置。
您还必须为 ListView 设置 MinHeight。
我的解决方案是创建您自己的扩展StackPanel 的面板类,覆盖MeasureOverride 和ArrangeOverride 函数。并将 ListView 添加到创建的面板中
自定义面板:
public class ScrollablePanel : StackPanel
{
protected override Size MeasureOverride(Size constraint)
{
Size tmpSize = base.MeasureOverride(constraint);
tmpSize.Height = (double)(this.Children[0] as UIElement).GetValue(MinHeightProperty);
return tmpSize;
}
protected override System.Windows.Size ArrangeOverride(System.Windows.Size finalSize)
{
Size tmpSize = new Size(0, 0);
//Width stays the same
tmpSize.Width = finalSize.Width;
//Height is changed
tmpSize.Height = finalSize.Height;
//This works only for one child!
this.Children[0].SetCurrentValue(HeightProperty, tmpSize.Height);
this.Children[0].Arrange(new Rect(new Point(0, 0), tmpSize));
return tmpSize;
}
}
XAML
<Window x:Class="WpfTest1.ScrollTestWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:WpfTest1"
Title="ScrollTestWindow" Height="400" Width="700">
<ScrollViewer >
<DockPanel LastChildFill="True" ScrollViewer.CanContentScroll="True" ScrollViewer.HorizontalScrollBarVisibility="Visible">
<Grid DockPanel.Dock="Top">
<TextBlock Text="Example controls above listview" Background="LightGray" FontSize="30"></TextBlock>
</Grid>
<Grid DockPanel.Dock="Bottom">
<TextBlock Text="Example controls below listview" Background="LightGray" FontSize="30"></TextBlock>
</Grid>
<local:ScrollablePanel>
<ListView FontSize="30" MinHeight="80">
<ListView.View>
<GridView>
<GridViewColumn Width="190" Header="Date" />
<GridViewColumn Width="200" Header="Day Of Week" DisplayMemberBinding="{Binding DayOfWeek}" />
<GridViewColumn Width="120" Header="Year" DisplayMemberBinding="{Binding Year}" />
</GridView>
</ListView.View>
<sys:DateTime>1/1/1</sys:DateTime>
<sys:DateTime>1/1/1</sys:DateTime>
<sys:DateTime>1/1/1</sys:DateTime>
<sys:DateTime>1/1/1</sys:DateTime>
<sys:DateTime>1/1/1</sys:DateTime>
<sys:DateTime>1/1/1</sys:DateTime>
<sys:DateTime>1/1/1</sys:DateTime>
<sys:DateTime>1/1/1</sys:DateTime>
</ListView>
</local:ScrollablePanel>
</DockPanel>
</ScrollViewer>
</Window>
很久以前有人问过这个问题,但我希望这个答案至少能帮助到一些人!
我还要感谢@sisyphe对于解决此问题所需的所有帮助:)
关于c# - WPF:Scrollviewer里面的ListView;布局问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4154307/