ToolTip on moving thumb for slider
I try to find a solution for having in slider a tooltip moving under thumb when i drag it
after reading somes post like :https://stackoverflow.com/questions/1178134/wpf-adding-a-tooltip-to-the-track-of-a-slider
i think that restyle slider is not a good solution when you using already restyled slider as mahapp for example
and microsoft good explain for tooltip position : https://docs.microsoft.com/fr-fr/dotnet/framework/wpf/controls/how-to-position-a-tooltip
So i find a better solution (more simple) with an attached property
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
public class SliderHelper { #region ShowToolTipOnMoving (ATTACHED) public static bool GetShowToolTipOnMoving(DependencyObject obj) => (bool)obj.GetValue(ShowToolTipOnMovingProperty); public static void SetShowToolTipOnMoving(DependencyObject obj, bool value) => obj.SetValue(ShowToolTipOnMovingProperty, value); // Using a DependencyProperty as the backing store for ShowToolTipOnMoving. This enables animation, styling, binding, etc... public static readonly DependencyProperty ShowToolTipOnMovingProperty = DependencyProperty.RegisterAttached( "ShowToolTipOnMoving", typeof(bool), typeof(SliderHelper), new FrameworkPropertyMetadata(false //,FrameworkPropertyMetadataOptions.AffectsRender , (d, e) => { if (d is Slider slider) { slider.Loaded += (ss, ee) => { // find thumb ToolTip tooltip = new ToolTip(); Thumb thumb = FindVisualChildren<Thumb>(slider).First(); thumb.DragStarted += (sss, eee) => { tooltip.Content = (int)slider.Value; tooltip.HorizontalOffset = 0; tooltip.Placement = PlacementMode.Bottom; tooltip.PlacementTarget = thumb; tooltip.PlacementRectangle = new Rect(-(thumb.ActualWidth / 2), thumb.ActualHeight + 2, 0, 0); tooltip.IsOpen = true; tooltip.StaysOpen = true; }; thumb.DragCompleted += (sss, eee) => { tooltip.IsOpen = false; tooltip.StaysOpen = false; }; thumb.DragDelta += (sss, eee) => { tooltip.Content = (int)slider.Value; tooltip.HorizontalOffset += 0.1; tooltip.HorizontalOffset -= 0.1; }; }; } } )); #endregion #region STATIC Functions public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject { if (depObj != null) { for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++) { DependencyObject child = VisualTreeHelper.GetChild(depObj, i); if (child != null && child is T) { yield return (T)child; } foreach (T childOfChild in FindVisualChildren<T>(child)) { yield return childOfChild; } } } } #endregion } |
and using like this
1 2 3 4 5 6 7 8 9 10 |
<Slider HorizontalAlignment="Left" Height="29" Margin="37,293,0,0" TickFrequency="1" IsSnapToTickEnabled="True" VerticalAlignment="Top" Value="5" Width="350" local:SliderHelper.ShowToolTipOnMoving="true" ToolTip="{Binding Value, RelativeSource={RelativeSource Self}}" /> |
we can find many improvment about this helper, but this is the first draft
Categories: Non classé
Commentaires récents