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
Next Next commit
Add attached property to allow slider tool tip formatting
  • Loading branch information
cjmurph committed May 13, 2022
commit da7d8199e2363d184300cef787d125869a619c05
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System.Globalization;
using Xunit;

namespace MaterialDesignThemes.Wpf.Tests.Converters
{
public class SliderToolTipConverterTests
{
[Theory]
[InlineData(1.4)]
[InlineData(-47.4)]
[InlineData(128.678)]
[InlineData(42)]
public void SliderConverterTest(object value)
{
Wpf.Converters.SliderToolTipConverter converter = new Wpf.Converters.SliderToolTipConverter();

//test a valid case
var result = converter.Convert(new object[] { value, "Test String Format {0}" }, typeof(string), null, CultureInfo.CurrentCulture);
Assert.Equal($"Test String Format {value}", result);

//test too many placeholders in format string
result = converter.Convert(new object[] { value, "{0} {1}" }, typeof(string), null, CultureInfo.CurrentCulture);
Assert.Equal(value.ToString(), result);

result = converter.Convert(new object[] { value, "{0} {1} {2}" }, typeof(string), null, CultureInfo.CurrentCulture);
Assert.Equal(value.ToString(), result);

//test empty format string
result = converter.Convert(new object[] { value, "" }, typeof(string), null, CultureInfo.CurrentCulture);
Assert.Equal(value.ToString(), result);

//test null format string
result = converter.Convert(new object[] { value, null }, typeof(string), null, CultureInfo.CurrentCulture);
Assert.Equal(value.ToString(), result);
}
}
}
24 changes: 24 additions & 0 deletions MaterialDesignThemes.Wpf/Converters/SliderToolTipConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Windows.Data;

namespace MaterialDesignThemes.Wpf.Converters
{
public class SliderToolTipConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values.Length >= 2 && values[1] is string format && !string.IsNullOrEmpty(format))
try
{
return string.Format(culture, format, values[0]);
}
catch (FormatException) { }
return System.Convert.ChangeType(values[0], targetType, culture);
}

public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
=> throw new NotImplementedException();
}
}
13 changes: 13 additions & 0 deletions MaterialDesignThemes.Wpf/SliderAssist.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,18 @@ public static void SetOnlyShowFocusVisualWhileDragging(RangeBase element, bool v

public static bool GetOnlyShowFocusVisualWhileDragging(RangeBase element)
=> (bool)element.GetValue(OnlyShowFocusVisualWhileDraggingProperty);

public static readonly DependencyProperty ToolTipFormatProperty
= DependencyProperty.RegisterAttached(
"ToolTipFormat",
typeof(string),
typeof(SliderAssist),
new PropertyMetadata(null));

public static void SetToolTipFormat(RangeBase element, string value)
=> element.SetValue(ToolTipFormatProperty, value);

public static string GetToolTipFormat(RangeBase element)
=> (string)element.GetValue(ToolTipFormatProperty);
}
}
21 changes: 17 additions & 4 deletions MaterialDesignThemes.Wpf/Themes/MaterialDesignTheme.Slider.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<converters:SliderValueLabelPositionConverter x:Key="SliderValueLabelPositionConverter" />
<converters:BooleanAllConverter x:Key="BooleanAllConverter" />
<converters:InvertBooleanConverter x:Key="InvertBooleanConverter" />
<converters:SliderToolTipConverter x:Key="SliderToolTipConverter" />

<Style x:Key="MaterialDesignRepeatButton" TargetType="{x:Type RepeatButton}">
<Setter Property="OverridesDefaultStyle" Value="True"/>
Expand Down Expand Up @@ -267,9 +268,15 @@
<TextBlock
Foreground="{DynamicResource MaterialDesignPaper}"
Margin="12,0,12,5"
Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=RangeBase}, Path=Value}"
TextAlignment="Center"
VerticalAlignment="Center" />
VerticalAlignment="Center">
<TextBlock.Text >
<MultiBinding Converter="{StaticResource SliderToolTipConverter}" ValidatesOnDataErrors="True" NotifyOnValidationError="True" TargetNullValue="">
<Binding RelativeSource="{RelativeSource FindAncestor, AncestorType=RangeBase}" Path="Value" TargetNullValue="" />
<Binding Path="(wpf:SliderAssist.ToolTipFormat)" RelativeSource="{RelativeSource FindAncestor, AncestorType=RangeBase}" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</Grid>
</Canvas>
<AdornerDecorator>
Expand Down Expand Up @@ -451,9 +458,15 @@
<TextBlock
Foreground="{DynamicResource MaterialDesignPaper}"
Margin="12,0,17,0"
Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=RangeBase}, Path=Value}"
TextAlignment="Center"
VerticalAlignment="Center" />
VerticalAlignment="Center">
<TextBlock.Text >
<MultiBinding Converter="{StaticResource SliderToolTipConverter}" ValidatesOnDataErrors="True" NotifyOnValidationError="True" TargetNullValue="">
<Binding RelativeSource="{RelativeSource FindAncestor, AncestorType=RangeBase}" Path="Value" TargetNullValue="" />
<Binding Path="(wpf:SliderAssist.ToolTipFormat)" RelativeSource="{RelativeSource FindAncestor, AncestorType=RangeBase}" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</Grid>
</Canvas>
<AdornerDecorator>
Expand Down