Last active
October 14, 2021 11:33
-
-
Save OlsonAndrewD/5623505 to your computer and use it in GitHub Desktop.
Ever wanted to write some XAML like Text="{Binding SomeValue, StringFormat={Binding SomeFormat}}"? You can't. But here's a way to get close, using attached properties.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class StringFormatHelper | |
{ | |
#region Value | |
public static DependencyProperty ValueProperty = DependencyProperty.RegisterAttached( | |
"Value", typeof(object), typeof(StringFormatHelper), new System.Windows.PropertyMetadata(null, OnValueChanged)); | |
private static void OnValueChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args) | |
{ | |
RefreshFormattedValue(obj); | |
} | |
public static object GetValue(DependencyObject obj) | |
{ | |
return obj.GetValue(ValueProperty); | |
} | |
public static void SetValue(DependencyObject obj, object newValue) | |
{ | |
obj.SetValue(ValueProperty, newValue); | |
} | |
#endregion | |
#region Format | |
public static DependencyProperty FormatProperty = DependencyProperty.RegisterAttached( | |
"Format", typeof(string), typeof(StringFormatHelper), new System.Windows.PropertyMetadata(null, OnFormatChanged)); | |
private static void OnFormatChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args) | |
{ | |
RefreshFormattedValue(obj); | |
} | |
public static string GetFormat(DependencyObject obj) | |
{ | |
return (string)obj.GetValue(FormatProperty); | |
} | |
public static void SetFormat(DependencyObject obj, string newFormat) | |
{ | |
obj.SetValue(FormatProperty, newFormat); | |
} | |
#endregion | |
#region FormattedValue | |
public static DependencyProperty FormattedValueProperty = DependencyProperty.RegisterAttached( | |
"FormattedValue", typeof(string), typeof(StringFormatHelper), new System.Windows.PropertyMetadata(null)); | |
public static string GetFormattedValue(DependencyObject obj) | |
{ | |
return (string)obj.GetValue(FormattedValueProperty); | |
} | |
public static void SetFormattedValue(DependencyObject obj, string newFormattedValue) | |
{ | |
obj.SetValue(FormattedValueProperty, newFormattedValue); | |
} | |
#endregion | |
private static void RefreshFormattedValue(DependencyObject obj) | |
{ | |
var value = GetValue(obj); | |
var format = GetFormat(obj); | |
if (format != null) | |
{ | |
if (!format.StartsWith("{0:")) | |
{ | |
format = String.Format("{{0:{0}}}", format); | |
} | |
SetFormattedValue(obj, String.Format(format, value)); | |
} | |
else | |
{ | |
SetFormattedValue(obj, value == null ? String.Empty : value.ToString()); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- You can't do this. --> | |
<TextBlock Text="{Binding Value, StringFormat={Binding FormatString}}" /> | |
<!-- But you can do this. --> | |
<TextBlock | |
local:StringFormatHelper.Format="{Binding FormatString}" | |
local:StringFormatHelper.Value="{Binding Value}" | |
Text="{Binding (local:StringFormatHelper.FormattedValue)}" | |
/> |
Please be advised that this will only let you format with decimal and hex modifiers like N3 or X2. If you want to add extra text like "The average temperature is {0:N1} degrees Celsius" you have to remove the if-statement on line 71 to line 74.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for your gist! And thank you @sleepHD for correcting the binding source to Self.