Xaml
Файл MainWindow.xaml
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="300" Width="600">
<Grid>
<ScrollViewer>
<DataGrid AutoGenerateColumns="False"
IsReadOnly="True"
ItemsSource="{Binding MyData}"
FontSize="20">
<DataGrid.Columns>
<DataGridTextColumn Header="Customer" Binding="{Binding Customer}" Width="150" />
<DataGridTextColumn Header="Item" Binding="{Binding Item}" Width="150"/>
<DataGridTextColumn Header="Price" Binding="{Binding Price}" Width="150"/>
</DataGrid.Columns>
</DataGrid>
</ScrollViewer>
</Grid>
</Window>
C#
Файл MainWindow.xaml.cs
using System.Collections.ObjectModel;
using System.Windows;
namespace WpfApp1
{
public class MyShop
{
public string Customer { get; set; }
public string Item { get; set; }
public string Price { get; set; }
}
public partial class MainWindow : Window
{
public ObservableCollection<MyShop> MyData { get; set; }
public MainWindow()
{
InitializeComponent();
// for binding
this.DataContext = this;
// data for grid
MyData = new ObservableCollection<MyShop>()
{
new MyShop() {Customer="ACME", Item="Widget", Price="100"},
new MyShop() {Customer="ACME", Item="Widget", Price="100"},
new MyShop() {Customer="ACME", Item="Tyre", Price="200"},
new MyShop() {Customer="Amazon", Item="Pen", Price="50"},
new MyShop() {Customer="Amazon", Item="Paper", Price="20"},
new MyShop() {Customer="Autodesk", Item="CAD", Price="10"},
new MyShop() {Customer="Autodesk", Item="File", Price="40"},
new MyShop() {Customer="ABC", Item="Flowers", Price="503"},
new MyShop() {Customer="ABC", Item="Sand", Price="706"},
new MyShop() {Customer="ABC", Item="Sea", Price="160"},
new MyShop() {Customer="ABC", Item="Home", Price="50"},
new MyShop() {Customer="ABC", Item="Nintendo", Price="15"},
new MyShop() {Customer="ABC", Item="Eat", Price="23"},
};
}
}
}
Xaml
Файл MainWindow.xaml
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="300" Width="600">
<Grid>
<ScrollViewer>
<DataGrid AutoGenerateColumns="False"
IsReadOnly="True"
ItemsSource="{Binding MyData}"
FontSize="20"
PreviewMouseWheel="MyPreviewMouseWheel">
<DataGrid.Columns>
<DataGridTextColumn Header="Customer" Binding="{Binding Customer}" Width="150" />
<DataGridTextColumn Header="Item" Binding="{Binding Item}" Width="150"/>
<DataGridTextColumn Header="Price" Binding="{Binding Price}" Width="150"/>
</DataGrid.Columns>
</DataGrid>
</ScrollViewer>
</Grid>
</Window>
C#
Файл MainWindow.xaml.cs
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace WpfApp1
{
public class MyShop
{
public string Customer { get; set; }
public string Item { get; set; }
public string Price { get; set; }
}
public partial class MainWindow : Window
{
public ObservableCollection<MyShop> MyData { get; set; }
public MainWindow()
{
InitializeComponent();
// for binding
this.DataContext = this;
// data for grid
MyData = new ObservableCollection<MyShop>()
{
new MyShop() {Customer="ACME", Item="Widget", Price="100"},
new MyShop() {Customer="ACME", Item="Widget", Price="100"},
new MyShop() {Customer="ACME", Item="Tyre", Price="200"},
new MyShop() {Customer="Amazon", Item="Pen", Price="50"},
new MyShop() {Customer="Amazon", Item="Paper", Price="20"},
new MyShop() {Customer="Autodesk", Item="CAD", Price="10"},
new MyShop() {Customer="Autodesk", Item="File", Price="40"},
new MyShop() {Customer="ABC", Item="Flowers", Price="503"},
new MyShop() {Customer="ABC", Item="Sand", Price="706"},
new MyShop() {Customer="ABC", Item="Sea", Price="160"},
new MyShop() {Customer="ABC", Item="Home", Price="50"},
new MyShop() {Customer="ABC", Item="Nintendo", Price="15"},
new MyShop() {Customer="ABC", Item="Eat", Price="23"},
};
}
private void MyPreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
if (e.Handled)
return;
var dataGrid = sender as DataGrid;
if (dataGrid == null)
return;
var scrollViewer = dataGrid?.Parent as ScrollViewer;
if (scrollViewer == null)
return;
scrollViewer?.RaiseEvent(new MouseWheelEventArgs(e.MouseDevice, e.Timestamp, e.Delta)
{
RoutedEvent = UIElement.MouseWheelEvent,
Source = sender
});
e.Handled = true;
}
}
}