1

I have the need to give the user the ability to set their own Decimal Formating option in XAML.

I know how to set StringFormat into Binding, but I only know how to do it manually. How can I Bind the String Format value inside a binding.

Abhishek Shah
  • 145
  • 2
  • 11

2 Answers2

0

You can use ContentStringFormat property.

URL: https://msdn.microsoft.com/en-us/library/system.windows.controls.contentcontrol.contentstringformat.aspx

Use it like this:

<TextBox Text="{Binding MyFormat, UpdateSourceTrigger=PropertyChanged}" />
<Label Content="{Binding ValueToFormat}"
       ContentStringFormat="{Binding MyFormat}" />
janonimus
  • 1,911
  • 2
  • 11
  • 11
  • I am trying to use it for DataGrid Cell. Which does not have ContentStringFormat property. Only Labels do I belive. – Abhishek Shah Mar 31 '17 at 13:27
  • If that's the case, you can just do the formatting in the `ViewModel`, manually. Or, you can use a `DataGridTemplateColumn`, and put a `TextBox` under the `DataGridTemplateColumn.CellTemplate`. – janonimus Apr 01 '17 at 00:13
0

How can I Bind the String Format value inside a binding.

You cannot because StringFormat is not a dependency property.

But you could use a multi binding that binds to two properties, the actual source property and the format source property, and an IMultiValueConverter class:

WPF Binding and Dynamically Assigning StringFormat Property

WPF: MultiBinding and IMultiValueConverter: https://blog.csainty.com/2009/12/wpf-multibinding-and.html

Community
  • 1
  • 1
mm8
  • 163,881
  • 10
  • 57
  • 88