我正在制作一个有 2 个数据网格的 wpf 应用程序,我希望它们一起滚动。我知道 DataGridView 类有一个滚动事件,您可以使用它对其他网格进行必要的更改,但 DataGrids 没有滚动事件。我必须使用 DataGrid。
这个例子很好,但不是 WPF,它使用 DataGridView 而不是 DataGrid。使用一个滚动条控制两个DataGridView
拥有它的最佳方法是什么,以便一个数据网格的滚动条也将在 WPF 和 DataGrids 中移动数据网格的滚动条?
您可以通过获取两个 DataGrid 的底层 ScrollViewer 并相应地设置事件来做到这一点。下面是我做的一个简单的例子,它看起来像你描述的那样工作。
xml:
<Window x:Class="WpfApplication1.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" Loaded="Window_Loaded">
<Grid>
<DataGrid AutoGenerateColumns="True" Height="200" HorizontalAlignment="Left" Margin="52,69,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="200" ItemsSource="{Binding Collection}" />
<DataGrid AutoGenerateColumns="True" Height="200" HorizontalAlignment="Left" Margin="270,69,0,0" Name="dataGrid2" VerticalAlignment="Top" Width="200" ItemsSource="{Binding Collection}" />
</Grid>
</Window>
后面的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
ObservableCollection<Person> _collection = new ObservableCollection<Person>();
ScrollViewer scrollView = null;
ScrollViewer scrollView2 = null;
public MainWindow()
{
for (int i = 0; i < 50; i++)
{
var p = new Person() { Name = string.Format("{0}", i), Age = i };
_collection.Add(p);
}
this.DataContext = this;
InitializeComponent();
}
void scrollView_ScrollChanged(object sender, ScrollChangedEventArgs e)
{
var newOffset = e.VerticalOffset;
if ((null != scrollView) && (null != scrollView2))
{
scrollView.ScrollToVerticalOffset(newOffset);
scrollView2.ScrollToVerticalOffset(newOffset);
}
}
public ObservableCollection<Person> Collection
{
get
{
return _collection;
}
}
private ScrollViewer getScrollbar(DependencyObject dep)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(dep); i++)
{
var child = VisualTreeHelper.GetChild(dep, i);
if ((null != child) && child is ScrollViewer)
{
return (ScrollViewer)child;
}
else
{
ScrollViewer sub = getScrollbar(child);
if (sub != null)
{
return sub;
}
}
}
return null;
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
scrollView = getScrollbar(dataGrid1);
scrollView2 = getScrollbar(dataGrid2);
if (null != scrollView)
{
scrollView.ScrollChanged += new ScrollChangedEventHandler(scrollView_ScrollChanged);
}
if (null != scrollView2)
{
scrollView2.ScrollChanged += new ScrollChangedEventHandler(scrollView_ScrollChanged);
}
}
}
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
}
发生的事情是我在使用 getScrollbar 加载窗口时遍历两个 DataGrids 的 VisualTree。然后我为两个 DataGrid 设置了滚动更改事件,然后滚动到刚刚在滚动更改事件处理程序中更改的垂直偏移量。