我正在尝试创建一个类似于macOS Finder的列视图触摸屏界面,它是一系列水平堆叠的列表,其中每个列表都可以单独滚动(垂直),整个界面可以水平滚动,就像这样:
这是我的.NET 4.6.1“最小可行代码示例”,以演示我正在做什么:
前端:
<Window x:Class="TestNestedScroll.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestNestedScroll"
Title="MainWindow" Height="500" Width="800"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Disabled" PanningMode="HorizontalOnly">
<ItemsControl ItemsSource="{Binding Columns}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled" PanningMode="VerticalOnly">
<ItemsControl ItemsSource="{Binding Rows}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Rectangle Width="300" Height="100" Fill="Purple" Margin="20"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
</Window>
后端:
using System.Collections.Generic;
using System.Linq;
using System.Windows;
namespace TestNestedScroll
{
public partial class MainWindow : Window
{
public class Row {}
public class Column { public List<Row> Rows { get; } = Enumerable.Repeat( new Row(), 20 ).ToList(); }
public List<Column> Columns { get; } = Enumerable.Repeat( new Column(), 10 ).ToList();
public MainWindow()
{
InitializeComponent();
}
}
}
目前我只能让这种方式起作用 -- 要么关闭内部滚动视图的PanningMode
,这样我就可以左右滚动外部ScrollViewer
,要么在内部滚动视图上设置 PanningMode="VerticalOnly"
(或Both
,或VerticalFirst
,不重要),它们变成独立的垂直滚动,但水平ScrollViewer
就停止工作了。
有办法让它工作吗?也许需要捕获内部ScrollViewers
上的水平触摸事件,并手动将其冒泡到父ScrollViewer
中 -- 我该如何做呢?