Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
added a new AP to SliderAssist to add an eventhandler to the RepeatBu…
…ttons of the Slider
  • Loading branch information
Corvin Szimion committed Oct 28, 2024
commit e1a64878ce55897d585b16542833b21bb70690cd
46 changes: 45 additions & 1 deletion src/MaterialDesignThemes.Wpf/SliderAssist.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace MaterialDesignThemes.Wpf;
using System.Windows.Media;

namespace MaterialDesignThemes.Wpf;

public static class SliderAssist
{
Expand Down Expand Up @@ -39,4 +41,46 @@ public static void SetToolTipFormat(RangeBase element, string value)

public static string GetToolTipFormat(RangeBase element)
=> (string)element.GetValue(ToolTipFormatProperty);

#region Issue3628
internal static readonly DependencyProperty FocusParentSliderOnClickProperty =
DependencyProperty.RegisterAttached(
"FocusParentSliderOnClick",
typeof(bool),
typeof(SliderAssist),
new PropertyMetadata(false, OnFocusParentSliderOnClickChanged));

internal static bool GetFocusParentSliderOnClick(DependencyObject obj) =>
(bool)obj.GetValue(FocusParentSliderOnClickProperty);

internal static void SetFocusParentSliderOnClick(DependencyObject obj, bool value) =>
obj.SetValue(FocusParentSliderOnClickProperty, value);

private static void OnFocusParentSliderOnClickChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is RepeatButton repeatButton)
{
if ((bool)e.NewValue)
{
repeatButton.AddHandler(UIElement.PreviewMouseLeftButtonDownEvent,
(MouseButtonEventHandler)RepeatButton_PreviewMouseLeftButtonDown,
true);
}
else
{
repeatButton.RemoveHandler(UIElement.PreviewMouseLeftButtonDownEvent,
(MouseButtonEventHandler)RepeatButton_PreviewMouseLeftButtonDown);
}
}
}

private static void RepeatButton_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (sender is DependencyObject repeatButton)
{
var slider = TreeHelper.FindParent<Slider>(repeatButton);
slider?.Focus();
}
}
#endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<Setter Property="Focusable" Value="False" />
<Setter Property="IsTabStop" Value="False" />
<Setter Property="OverridesDefaultStyle" Value="True" />
<Setter Property="wpf:SliderAssist.FocusParentSliderOnClick" Value="True"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type RepeatButton}">
Expand Down Expand Up @@ -965,7 +966,7 @@
<Setter Property="BorderBrush" Value="Transparent" />
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
<Setter Property="Foreground" Value="{DynamicResource MaterialDesign.Brush.Primary}" />
<Setter Property="IsMoveToPointEnabled" Value="False" />
<Setter Property="IsMoveToPointEnabled" Value="True" />
<Setter Property="SnapsToDevicePixels" Value="False" />
<Setter Property="Stylus.IsPressAndHoldEnabled" Value="false" />
<Setter Property="Template" Value="{StaticResource MaterialDesignSliderHorizontal}" />
Expand All @@ -985,12 +986,7 @@
<Setter Property="Background" Value="{x:Null}" />
<Setter Property="BorderBrush" Value="Transparent" />
<Setter Property="Foreground" Value="{DynamicResource MaterialDesign.Brush.Primary}" />
<!--
When IsMoveToPointEnabled is true, additional code is being executed before the actual "base.OnPreviewMouseLeftButtonDown(e);"
in which the event is marked as handled, i suspect this is the issue, see:
https://source.dot.net/#PresentationFramework/System/Windows/Controls/Slider.cs,771
-->
<Setter Property="IsMoveToPointEnabled" Value="False" />
<Setter Property="IsMoveToPointEnabled" Value="True" />
<Setter Property="IsSnapToTickEnabled" Value="True" />
<Setter Property="Orientation" Value="Horizontal" />
<Setter Property="SnapsToDevicePixels" Value="False" />
Expand Down
18 changes: 18 additions & 0 deletions src/MaterialDesignThemes.Wpf/TreeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,22 @@ private static bool IsAncestorTill(FrameworkElement? element, object ancestor, o
}
return foundChild;
}

public static T FindParent<T>(DependencyObject child) where T : DependencyObject
{
DependencyObject parentObject = VisualTreeHelper.GetParent(child);
if (parentObject is null)
{
return null!;
}

if (parentObject is T parent)
{
return parent;
}
else
{
return FindParent<T>(parentObject);
}
}
}