Skip to content

Commit ce709aa

Browse files
committed
KB-20838-Sample, README.md updated
1 parent 67a82c6 commit ce709aa

17 files changed

+668
-2
lines changed

README.md

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,40 @@
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 (SfDataGrid)
2+
3+
4+
In [WPF DataGrid](https://www.syncfusion.com/wpf-controls/datagrid) (SfDataGrid), vertical scrolling moves one full row at a time. You can modify this behavior to scroll by your preferred units by adjusting the vertical offset in the [ScrollChanged](https://learn.microsoft.com/en-us/dotnet/api/system.windows.controls.scrollviewer.scrollchanged?view=netframework-4.7.2) event.
5+
6+
**Code snippet to scroll vertically by less than a full row:**
7+
8+
9+
```csharp
10+
sfDataGrid.Loaded += OnLoaded;
11+
12+
private void OnLoaded(object sender, RoutedEventArgs e)
13+
{
14+
this.sfDataGrid.GetVisualContainer().ScrollOwner.ScrollChanged += OnScrollChanged;
15+
}
16+
17+
private void OnScrollChanged(object sender, ScrollChangedEventArgs e)
18+
{
19+
if (e.VerticalChange == 24)
20+
{
21+
if (e.VerticalChange > 0)
22+
{
23+
// Here customize based on your scenario
24+
double newOffset = e.VerticalOffset - 12;
25+
this.sfDataGrid.GetVisualContainer().SetVerticalOffset(newOffset);
26+
}
27+
}
28+
if (e.VerticalChange == -24)
29+
{
30+
if (e.VerticalChange < 0)
31+
{
32+
// Here customize based on your scenario
33+
double newOffset = e.VerticalOffset + 12;
34+
this.sfDataGrid.GetVisualContainer().SetVerticalOffset(newOffset);
35+
}
36+
}
37+
}
38+
```
39+
40+
![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: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace SfDataGrid_Demo.Model
8+
{
9+
public class OrderInfo
10+
{
11+
int orderID;
12+
string customerId;
13+
string country;
14+
string customerName;
15+
string shippingCity;
16+
17+
public int OrderID
18+
{
19+
get { return orderID; }
20+
set { orderID = value; }
21+
}
22+
23+
public string CustomerID
24+
{
25+
get { return customerId; }
26+
set { customerId = value; }
27+
}
28+
29+
public string CustomerName
30+
{
31+
get { return customerName; }
32+
set { customerName = value; }
33+
}
34+
35+
public string Country
36+
{
37+
get { return country; }
38+
set { country = value; }
39+
}
40+
41+
public string ShipCity
42+
{
43+
get { return shippingCity; }
44+
set { shippingCity = value; }
45+
}
46+
47+
public OrderInfo(int orderId, string customerName, string country, string customerId, string shipCity)
48+
{
49+
this.OrderID = orderId;
50+
this.CustomerName = customerName;
51+
this.Country = country;
52+
this.CustomerID = customerId;
53+
this.ShipCity = shipCity;
54+
}
55+
}
56+
}
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)