[ColorPicker]Fixed Scrolling through the history in the color picker editor with a mouse wheel (#33551)

## Summary of the Pull Request
Fixed Scrolling through the history in the color picker editor with a
mouse wheel

## Detailed Description of the Pull Request / Additional comments
I added a mousewheel event listener on the HistoryColors ListView, then
I added two functions in the ColorEditorView.xaml.cs to handle the
Event, it checks the number of color in the history and handles the
scroll control accordingly
This commit is contained in:
Fefe_du_973 2024-07-25 18:06:12 +02:00 committed by GitHub
parent 4fee37c35a
commit 63625a1cee
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 56 additions and 0 deletions

View File

@ -48,6 +48,7 @@
AutomationProperties.Name="{x:Static p:Resources.Color_History}"
ItemsSource="{Binding ColorsHistory}"
KeyboardNavigation.DirectionalNavigation="Contained"
MouseWheel="HistoryColors_OnMouseWheelScroll"
ScrollViewer.HorizontalScrollBarVisibility="Auto"
SelectedIndex="{Binding SelectedColorIndex}"
SelectionMode="Extended"

View File

@ -5,6 +5,7 @@
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using ColorPicker.Helpers;
using ColorPicker.ViewModels;
@ -42,6 +43,60 @@ namespace ColorPicker.Views
};
}
/// <summary>
/// Handles the mouse wheel scroll event on the HistoryColors ListView.
/// Scrolls the ListView horizontally based on the direction of the mouse wheel scroll.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The mouse wheel event data.</param>
private void HistoryColors_OnMouseWheelScroll(object sender, System.Windows.Input.MouseWheelEventArgs e)
{
var scrollViewer = FindVisualChild<ScrollViewer>(HistoryColors);
if (scrollViewer != null)
{
if (e.Delta > 0)
{
scrollViewer.LineLeft();
}
else
{
scrollViewer.LineRight();
}
e.Handled = true;
}
}
/// <summary>
/// Finds a visual child of a specified type within a given dependency object.
/// </summary>
/// <typeparam name="T">The type of the child element to find.</typeparam>
/// <param name="obj">The parent dependency object.</param>
/// <returns>The first child element of the specified type, or null if no such element is found.</returns>
private static T FindVisualChild<T>(DependencyObject obj)
where T : DependencyObject
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(obj, i);
if (child != null && child is T tChild)
{
return tChild;
}
else
{
T childOfChild = FindVisualChild<T>(child);
if (childOfChild != null)
{
return childOfChild;
}
}
}
return null;
}
/*
private void HistoryColors_ItemClick(object sender, ItemClickEventArgs e)
{