Abhishek Shah
Abhishek Shah

Reputation: 145

WPF XAML StringFormat assign amount of decimal places through binding

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.

Upvotes: 1

Views: 1525

Answers (2)

mm8
mm8

Reputation: 169200

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

Upvotes: 0

janonimus
janonimus

Reputation: 2877

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}" />

Upvotes: 0

Related Questions