From ef56dcacf07a715a7f20ff76a43de1f5b73b573d Mon Sep 17 00:00:00 2001 From: peerpalo Date: Thu, 7 Sep 2023 17:32:45 +0200 Subject: [PATCH] [ColorPicker]Scroll color history to top after choosing new color (#28291) --- .../Views/ColorEditorView.xaml.cs | 29 ++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/modules/colorPicker/ColorPickerUI/Views/ColorEditorView.xaml.cs b/src/modules/colorPicker/ColorPickerUI/Views/ColorEditorView.xaml.cs index 16cb80bf0d..ba17e3be86 100644 --- a/src/modules/colorPicker/ColorPickerUI/Views/ColorEditorView.xaml.cs +++ b/src/modules/colorPicker/ColorPickerUI/Views/ColorEditorView.xaml.cs @@ -2,8 +2,11 @@ // The Microsoft Corporation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System.ComponentModel; +using System.Windows; using System.Windows.Controls; using ColorPicker.Helpers; +using ColorPicker.ViewModels; namespace ColorPicker.Views { @@ -12,8 +15,32 @@ namespace ColorPicker.Views /// public partial class ColorEditorView : UserControl { - public ColorEditorView() => + public ColorEditorView() + { InitializeComponent(); + Loaded += OnLoaded; + } + + private void OnLoaded(object sender, RoutedEventArgs e) + { + EnableHistoryColorsScrollIntoView(); + } + + /// + /// 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. + /// + 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) {