Skip to content

Instantly share code, notes, and snippets.

View bjarnesvanberg's full-sized avatar

Bjarne Svanberg bjarnesvanberg

View GitHub Profile
@OlsonAndrewD
OlsonAndrewD / StringFormatHelper.cs
Last active October 14, 2021 11:33
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.
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);