Skip to content

Commit 0b11f6a

Browse files
authored
Merge pull request #1 from SyncfusionExamples/KB-20838
KB-20838
2 parents 67a82c6 + 2abfe4b commit 0b11f6a

16 files changed

+660
-2
lines changed

README.md

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,37 @@
1-
# How-to-scroll-vertically-by-less-than-a-full-row-in-a-WPF-DataGrid
2-
How to scroll vertically by less than a full row in a WPF DataGrid (SfDataGrid)
1+
# How to scroll vertically by less than a full row in a WPF DataGrid?
2+
3+
4+
In [WPF DataGrid](https://www.syncfusion.com/wpf-controls/datagrid) (SfDataGrid), vertical scrolling by default advances one full row at a time. This behavior can be customized to scroll by specific units by adjusting the vertical offset within the `ScrollChanged` event.
5+
6+
```csharp
7+
sfDataGrid.Loaded += OnLoaded;
8+
9+
private void OnLoaded(object sender, RoutedEventArgs e)
10+
{
11+
this.sfDataGrid.GetVisualContainer().ScrollOwner.ScrollChanged += OnScrollChanged;
12+
}
13+
14+
private void OnScrollChanged(object sender, ScrollChangedEventArgs e)
15+
{
16+
if (e.VerticalChange == 24)
17+
{
18+
if (e.VerticalChange > 0)
19+
{
20+
// Here customize based on your scenario
21+
double newOffset = e.VerticalOffset - 12;
22+
this.sfDataGrid.GetVisualContainer().SetVerticalOffset(newOffset);
23+
}
24+
}
25+
if (e.VerticalChange == -24)
26+
{
27+
if (e.VerticalChange < 0)
28+
{
29+
// Here customize based on your scenario
30+
double newOffset = e.VerticalOffset + 12;
31+
this.sfDataGrid.GetVisualContainer().SetVerticalOffset(newOffset);
32+
}
33+
}
34+
}
35+
```
36+
37+
![VerticalScroll](VerticalScroll.gif)

SfDataGrid_Demo/App.config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
5+
</startup>
6+
</configuration>

SfDataGrid_Demo/App.xaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Application x:Class="SfDataGrid_Demo.App"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:local="clr-namespace:SfDataGrid_Demo"
5+
StartupUri="MainWindow.xaml">
6+
<Application.Resources>
7+
8+
</Application.Resources>
9+
</Application>

SfDataGrid_Demo/App.xaml.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Configuration;
4+
using System.Data;
5+
using System.Linq;
6+
using System.Threading.Tasks;
7+
using System.Windows;
8+
9+
namespace SfDataGrid_Demo
10+
{
11+
/// <summary>
12+
/// Interaction logic for App.xaml
13+
/// </summary>
14+
public partial class App : Application
15+
{
16+
}
17+
}

SfDataGrid_Demo/MainWindow.xaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Window x:Class="SfDataGrid_Demo.MainWindow"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5+
xmlns:syncfusion="http://schemas.syncfusion.com/wpf"
6+
xmlns:View="clr-namespace:SfDataGrid_Demo.ViewModel"
7+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
8+
xmlns:local="clr-namespace:SfDataGrid_Demo"
9+
mc:Ignorable="d"
10+
Title="MainWindow" Height="450" Width="800">
11+
<Window.DataContext>
12+
<View:ViewModel />
13+
</Window.DataContext>
14+
<Grid>
15+
<syncfusion:SfDataGrid x:Name="sfDataGrid" ItemsSource="{Binding Orders}" Margin="0,0,91,127" />
16+
</Grid>
17+
</Window>

SfDataGrid_Demo/MainWindow.xaml.cs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using Syncfusion.UI.Xaml.Grid.Helpers;
2+
using Syncfusion.UI.Xaml.ScrollAxis;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
using System.Windows;
9+
using System.Windows.Controls;
10+
using System.Windows.Data;
11+
using System.Windows.Documents;
12+
using System.Windows.Input;
13+
using System.Windows.Media;
14+
using System.Windows.Media.Imaging;
15+
using System.Windows.Navigation;
16+
using System.Windows.Shapes;
17+
using Syncfusion.UI.Xaml.ScrollAxis;
18+
using ScrollChangedEventArgs = System.Windows.Controls.ScrollChangedEventArgs;
19+
20+
namespace SfDataGrid_Demo
21+
{
22+
/// <summary>
23+
/// Interaction logic for MainWindow.xaml
24+
/// </summary>
25+
public partial class MainWindow : Window
26+
{
27+
28+
public MainWindow()
29+
{
30+
InitializeComponent();
31+
sfDataGrid.Loaded += OnLoaded;
32+
}
33+
34+
private void OnLoaded(object sender, RoutedEventArgs e)
35+
{
36+
this.sfDataGrid.GetVisualContainer().ScrollOwner.ScrollChanged += OnScrollChanged;
37+
}
38+
39+
private void OnScrollChanged(object sender, ScrollChangedEventArgs e)
40+
{
41+
if (e.VerticalChange == 24)
42+
{
43+
if (e.VerticalChange > 0)
44+
{
45+
// Here customize based on your scenario
46+
double newOffset = e.VerticalOffset - 12;
47+
this.sfDataGrid.GetVisualContainer().SetVerticalOffset(newOffset);
48+
}
49+
}
50+
if (e.VerticalChange == -24)
51+
{
52+
if (e.VerticalChange < 0)
53+
{
54+
//Here customize based on your scenario
55+
double newOffset = e.VerticalOffset + 12;
56+
this.sfDataGrid.GetVisualContainer().SetVerticalOffset(newOffset);
57+
}
58+
}
59+
}
60+
}
61+
}

SfDataGrid_Demo/Model/Model.cs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel;
4+
using System.Linq;
5+
using System.Runtime.CompilerServices;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace SfDataGrid_Demo.Model
10+
{
11+
public class OrderInfo : INotifyPropertyChanged
12+
{
13+
int orderID;
14+
string customerId;
15+
string country;
16+
string customerName;
17+
string shippingCity;
18+
public event PropertyChangedEventHandler PropertyChanged;
19+
20+
public int OrderID
21+
{
22+
get { return orderID; }
23+
set { orderID = value; OnPropertyChanged("OrderID"); }
24+
}
25+
26+
public string CustomerID
27+
{
28+
get { return customerId; }
29+
set { customerId = value; OnPropertyChanged("CustomerID"); }
30+
}
31+
32+
public string CustomerName
33+
{
34+
get { return customerName; }
35+
set { customerName = value; OnPropertyChanged("CustomerName"); }
36+
}
37+
38+
public string Country
39+
{
40+
get { return country; }
41+
set { country = value; OnPropertyChanged("Country"); }
42+
}
43+
44+
public string ShipCity
45+
{
46+
get { return shippingCity; }
47+
set { shippingCity = value; OnPropertyChanged("ShipCity"); }
48+
}
49+
50+
public OrderInfo(int orderId, string customerName, string country, string customerId, string shipCity)
51+
{
52+
this.OrderID = orderId;
53+
this.CustomerName = customerName;
54+
this.Country = country;
55+
this.CustomerID = customerId;
56+
this.ShipCity = shipCity;
57+
}
58+
59+
60+
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
61+
{
62+
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
63+
}
64+
65+
}
66+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System.Reflection;
2+
using System.Resources;
3+
using System.Runtime.CompilerServices;
4+
using System.Runtime.InteropServices;
5+
using System.Windows;
6+
7+
// General Information about an assembly is controlled through the following
8+
// set of attributes. Change these attribute values to modify the information
9+
// associated with an assembly.
10+
[assembly: AssemblyTitle("SfDataGrid_Demo")]
11+
[assembly: AssemblyDescription("")]
12+
[assembly: AssemblyConfiguration("")]
13+
[assembly: AssemblyCompany("")]
14+
[assembly: AssemblyProduct("SfDataGrid_Demo")]
15+
[assembly: AssemblyCopyright("Copyright © 2023")]
16+
[assembly: AssemblyTrademark("")]
17+
[assembly: AssemblyCulture("")]
18+
19+
// Setting ComVisible to false makes the types in this assembly not visible
20+
// to COM components. If you need to access a type in this assembly from
21+
// COM, set the ComVisible attribute to true on that type.
22+
[assembly: ComVisible(false)]
23+
24+
//In order to begin building localizable applications, set
25+
//<UICulture>CultureYouAreCodingWith</UICulture> in your .csproj file
26+
//inside a <PropertyGroup>. For example, if you are using US english
27+
//in your source files, set the <UICulture> to en-US. Then uncomment
28+
//the NeutralResourceLanguage attribute below. Update the "en-US" in
29+
//the line below to match the UICulture setting in the project file.
30+
31+
//[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]
32+
33+
34+
[assembly: ThemeInfo(
35+
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
36+
//(used if a resource is not found in the page,
37+
// or application resource dictionaries)
38+
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
39+
//(used if a resource is not found in the page,
40+
// app, or any theme specific resource dictionaries)
41+
)]
42+
43+
44+
// Version information for an assembly consists of the following four values:
45+
//
46+
// Major Version
47+
// Minor Version
48+
// Build Number
49+
// Revision
50+
//
51+
// You can specify all the values or you can default the Build and Revision Numbers
52+
// by using the '*' as shown below:
53+
// [assembly: AssemblyVersion("1.0.*")]
54+
[assembly: AssemblyVersion("1.0.0.0")]
55+
[assembly: AssemblyFileVersion("1.0.0.0")]

SfDataGrid_Demo/Properties/Resources.Designer.cs

Lines changed: 63 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)