WPF ItemsControl - How to adjust scrolling offset?
WPF ItemsControl - How to adjust scrolling offset?
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" SizeToContent="WidthAndHeight" UseLayoutRounding="True" WindowStartupLocation="CenterScreen" WindowStyle="ToolWindow">
<Window.Resources>
<local:Items x:Key="Items" />
</Window.Resources>
<ItemsControl ItemsSource="{Binding Source={StaticResource Items}}" Height="490" Width="100">
<ItemsControl.Template>
<ControlTemplate>
<ScrollViewer CanContentScroll="false">
<ItemsPresenter />
</ScrollViewer>
</ControlTemplate>
</ItemsControl.Template>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border BorderThickness="0,0,0,1" BorderBrush="Black">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding LineNum}" Width="20" VerticalAlignment="Center" />
<StackPanel>
<TextBlock Text="{Binding Line0}" />
<TextBlock Text="{Binding Line1}" />
<TextBlock Text="{Binding Line2}" />
</StackPanel>
</StackPanel>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Window>
cs...
using System.Collections.Generic;
using System.Windows;
namespace WpfApp1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
public class Item
{
public string LineNum { get; set; }
public static string Line0 => "Abcdefghi";
public static string Line1 => "Jklmnopqr";
public static string Line2 => "Styvwxyz!";
}
public class Items : List<Item>
{
public Items()
{
for (int i = 1; i <= 50; i++)
{
Item newItem = new()
{
LineNum = i.ToString(),
};
Add(newItem);
}
}
}
}
run...
issue...
With the border, each notch of the mouse wheel rolled scrolls one item, but yields an offset of the border thickness.
Setting the border thickness to 0 makes scrolling to be exactly one item, but borderless is not acceptable.
Setting CanContentScroll to true makes scrolling yield no offset, but the number of items per notch is determined by Windows mouse settings, with the default value of 3 lines resulting in 3 items in this case.
What I want is to be able to scroll exactly one item per notch, with the border, and independent of Windows settings.