[ColorPicker]Scroll color history to top after choosing new color (#28291)

This commit is contained in:
peerpalo 2023-09-07 17:32:45 +02:00 committed by GitHub
parent 16e105106b
commit ef56dcacf0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,8 +2,11 @@
// The Microsoft Corporation licenses this file to you under the MIT license. // The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information. // See the LICENSE file in the project root for more information.
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
using ColorPicker.Helpers; using ColorPicker.Helpers;
using ColorPicker.ViewModels;
namespace ColorPicker.Views namespace ColorPicker.Views
{ {
@ -12,8 +15,32 @@ namespace ColorPicker.Views
/// </summary> /// </summary>
public partial class ColorEditorView : UserControl public partial class ColorEditorView : UserControl
{ {
public ColorEditorView() => public ColorEditorView()
{
InitializeComponent(); InitializeComponent();
Loaded += OnLoaded;
}
private void OnLoaded(object sender, RoutedEventArgs e)
{
EnableHistoryColorsScrollIntoView();
}
/// <summary>
/// Updating SelectedColorIndex will not refresh ListView viewport.
/// We listen for SelectedColorIndex property change and in case of a value <= 0 (new color is added) a call to ScrollIntoView is made so ListView will scroll up.
/// </summary>
private void EnableHistoryColorsScrollIntoView()
{
ColorEditorViewModel colorEditorViewModel = (ColorEditorViewModel)this.DataContext;
((INotifyPropertyChanged)colorEditorViewModel).PropertyChanged += (sender, args) =>
{
if (args.PropertyName == nameof(colorEditorViewModel.SelectedColorIndex) && colorEditorViewModel.SelectedColorIndex <= 0)
{
HistoryColors.ScrollIntoView(colorEditorViewModel.SelectedColor);
}
};
}
private void HistoryColors_ItemClick(object sender, ModernWpf.Controls.ItemClickEventArgs e) private void HistoryColors_ItemClick(object sender, ModernWpf.Controls.ItemClickEventArgs e)
{ {