I created an attached property and converter for this. You probably already have the converter, so replace my reference to CaseConverter to whatever implementation you have.
The attached property is just a boolean that you set if you want it to be uppercase (you could obviously extend this to instead be an enumerable for a selection of styles). When the property changes, it rebinds the TextBlock's Text property as needed, adding in the converter.
A little more work might need to be done when the property is already bound - my solution assumes it's a simple Path binding. But it may need to also duplicate the source, etc. However I felt this example is enough to get my point across.
Here's the attached property:
public static bool GetUppercase(DependencyObject obj)
{
return (bool)obj.GetValue(UppercaseProperty);
}
public static void SetUppercase(DependencyObject obj, bool value)
{
obj.SetValue(UppercaseProperty, value);
}
// Using a DependencyProperty as the backing store for Uppercase. This enables animation, styling, binding, etc...
public static readonly DependencyProperty UppercaseProperty =
DependencyProperty.RegisterAttached("Uppercase", typeof(bool), typeof(TextHelper), new PropertyMetadata(false, OnUppercaseChanged));
private static void OnUppercaseChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
TextBlock txt = d as TextBlock;
if (txt == null) return;
var val = (bool)e.NewValue;
if (val)
{
// rebind the text using converter
// if already bound, use it as source
var original = txt.GetBindingExpression(TextBlock.TextProperty);
var b = new Binding();
if (original != null)
{
b.Path = original.ParentBinding.Path;
}
else
{
b.Source = txt.Text;
}
b.Converter = new CaseConverter() { Case = CharacterCasing.Upper };
txt.SetBinding(TextBlock.TextProperty, b);
}
}