Skip to content

Commit

Permalink
Inserrt ScrollIntoViewBehavior to scroll the list box
Browse files Browse the repository at this point in the history
  • Loading branch information
davideCappe committed Jun 3, 2024
1 parent e34a567 commit 543360e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/CodeSculpt.Wpf/Behaviors/ScrollIntoViewBehavior.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Windows.Controls;
using System.Windows.Threading;
using Microsoft.Xaml.Behaviors;

namespace CodeSculpt.Wpf.Behaviors;
public class ScrollIntoViewBehavior : Behavior<ListBox>
{
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.SelectionChanged += AssociatedObject_SelectionChanged;
}

private void AssociatedObject_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (sender is not ListBox control)
{
return;
}

if (control.SelectedItem is not null)
{
control.Dispatcher.BeginInvoke(DispatcherPriority.Render, () =>
{
control.UpdateLayout();
control.ScrollIntoView(control.SelectedItem);
});
}
}
protected override void OnDetaching()
{
base.OnDetaching();
AssociatedObject.SelectionChanged -= AssociatedObject_SelectionChanged;
}
}
1 change: 1 addition & 0 deletions src/CodeSculpt.Wpf/CodeSculpt.Wpf.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@


<ItemGroup>
<PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" Version="1.1.77" />
<PackageReference Include="Nerdbank.GitVersioning" Version="3.6.133">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down

0 comments on commit 543360e

Please sign in to comment.