Skip to content

Commit

Permalink
Merge pull request #107 from atc-net/feature/Improve-AutoScrollListVi…
Browse files Browse the repository at this point in the history
…ewBehavior

Feature/improve auto scroll list view behavior
  • Loading branch information
davidkallesen authored Feb 21, 2024
2 parents bf107a1 + a12a11b commit a145434
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
33 changes: 31 additions & 2 deletions src/Atc.Wpf.Controls/Behaviors/AutoScrollListViewBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,31 @@ namespace Atc.Wpf.Controls.Behaviors;

public class AutoScrollListViewBehavior : Behavior<ListView>
{
public bool ScrollToBottom { get; set; } = true;
public static readonly DependencyProperty ScrollDirectionProperty =
DependencyProperty.Register(
nameof(ScrollDirection),
typeof(ScrollDirectionType),
typeof(AutoScrollListViewBehavior),
new PropertyMetadata(ScrollDirectionType.Bottom));

public ScrollDirectionType ScrollDirection
{
get => (ScrollDirectionType)GetValue(ScrollDirectionProperty);
set => SetValue(ScrollDirectionProperty, value);
}

public static readonly DependencyProperty IsEnabledProperty =
DependencyProperty.Register(
nameof(IsEnabled),
typeof(bool),
typeof(AutoScrollListViewBehavior),
new PropertyMetadata(BooleanBoxes.FalseBox));

public bool IsEnabled
{
get => (bool)GetValue(IsEnabledProperty);
set => SetValue(IsEnabledProperty, value);
}

protected override void OnAttached()
{
Expand All @@ -22,13 +46,18 @@ private void OnCollectionChanged(
object? sender,
NotifyCollectionChangedEventArgs e)
{
if (!IsEnabled)
{
return;
}

var listView = AssociatedObject;
if (listView.Items.Count <= 1)
{
return;
}

var targetItem = ScrollToBottom
var targetItem = ScrollDirection == ScrollDirectionType.Bottom
? listView.Items[^1]!
: listView.Items[0]!;

Expand Down
8 changes: 8 additions & 0 deletions src/Atc.Wpf/Enums/ScrollDirectionType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// ReSharper disable CheckNamespace
namespace Atc.Wpf;

public enum ScrollDirectionType
{
Top,
Bottom,
}
2 changes: 1 addition & 1 deletion src/Atc.Wpf/Mvvm/ViewModelBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Atc.Wpf.Mvvm;

/// <summary>
/// A base class for a the ViewModel class, to be used in the MVVM pattern design.
/// A base class for the ViewModel class, to be used in the MVVM pattern design.
/// </summary>
public abstract class ViewModelBase : ObservableObject, IViewModelBase
{
Expand Down

0 comments on commit a145434

Please sign in to comment.