I found something like this:
How to set WPF string format as percent wihout multiplying by 100?
I just want to have a '%' sign suffix after my number. my code looks like this
<Label Content="{Binding Path=ReportGridViewModel.FillingDegree, StringFormat=P}" />
I already tried this one too
<Label Content="{Binding ReportGridViewModel.Cgi, StringFormat={}{0}%}" />
in both cases I don't see any changes like I don't have any stringformat.
The StringFormat property of a Binding is only applied when the target property is of type String. In a Label, the target property Content, is of type Object, so StringFormat is not respected.
To get this to work in a label, use ContentStringFormat. If you were to use a TextBlock, you could use the StringFormat provided by Binding.
In the case of Label you need to use the ContentStringFormat property
<Label Content="{Binding Path=ReportGridViewModel.FillingDegree}"
ContentStringFormat="P" />
Related
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.
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
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}" />
I want to display a number in a Label, it should be formatted with 2 decimal places (always).
Example:
<Label Content="{Binding MyMoneyAmount}" />
If MyMoneyAmount = 100, then it should display as: 100.00 not 100.
<Label Content="{Binding MyMoneyAmount, StringFormat={0:F2}}" />
According to this SO Does StringFormat work on Label Content and also my own testing, StringFormat won't work on a Label as Content is of type Object as reflected in another answer on this page.
You can use this workaround (to display for example as a currency):
<Label><TextBlock Text="{Binding Source={myValue}, StringFormat={}{0:N2}}" /></Label>
I have a textblock and I would like to bind its content to a property in my viewmodel. This is fine if the content is a simple string. But it's no so fine if I want to format the content and use or tags... In this case I cannot bind a string: the textblock would simply display a string like this "Hallo".
Any ideas ? Thanks
if you have a property of some type - you can create a datatemplate for this type
<DataTemplate DataType="{x:Type local:MySomeType}">
<!--your visual presentation goes here-->
</DataTemplate>
now you can simply use a ContentPresenter to show your property
<ContentPresenter Content="{Binding MySomeTypeProperty}"/>
See what the StringFormat property can do for you. If that is not sufficient, you might want to write a binding converter.
Something like this:
<Textblock content="{Binding MyProperty, StringFormat={}Hello {1}}" />
Just got to play with the string format.
I can't get my custom DateTime string format to work in my binding. I want the format to be "mmmm, yyyy" (e.g. "June, 2012").
The following does not work. I get a short date format (m/d/yyyy).
<TextBlock Text="{Binding ElementName=ThisWindow,
Path=Date,
StringFormat={}{0:MMMM\, yyyy}"/>
I've considered using a converter, but I prefer a pure XAML approach.
Edit:
For clarity, I have a Window with a dependency property Date of type DateTime. In my XAML, I've named the window 'Thiswindow'.
Edit 2:
I looked back at my actual code, and I had a Label, not a TextBlock. I changed it to TextBlock and it works fine.
<Label Content="{Binding ElementName=ThisWindow,
Path=Date,
StringFormat={}{0:MMMM\, yyyy}"/>
Anyone know why it doesn't work with Label?
Thanks.
ContentControls have a ContentStringFormat property which overrides the original formatting.
(When i saw your question i expected this to be the problem actually but was surprised to find a TextBlock at first)
Your month needs to be in uppercase:
{Binding Source={x:Static sys:DateTime.Now}, StringFormat={}{0:MMMM\, yyyy}}
EDIT:
The Label problem is probably because Label has Content, not Text.
Change the Text="{Binding ...}" to Content="{Binding ...}"
This should have a simple solution, but I can't seem to find it.
I want to do something like the following, where I have a data binding with a string format. The field is a text field, and I'd like to display it with a suffix (but not change the underlying data).
<Label Name="field" Content="{Binding obj.field, StringFormat=\{0\} suffix}" />
So I want obj.field's value, for instance "value", to display as "value suffix".
Is it really necessary to use a ValueConverter (or whatever) to do this? I'm thinking that if it's possible with the StringFormat construction, then there's some magic format option I just haven't encountered.
This leads to a related question: where can I find a reference for WPF StringFormat? I can find the reference for the c# String.Format formatting options, but these don't all seem to work in WPF (like what I've tried above).
StringFormat will work if the target type is string. However, the type of Label's Content property is object. That is why the StringFormat has no effect. If you put a TextBlock inside the Label (or only use a TextBlock) and bind the Textblock's Text property, it should work fine.
<Label>
<TextBlock Text="{Binding obj.field, StringFormat=\{0\} suffix}" />
</Label>
If you have other reasons to want to bind the value to the Label, you could also do the following.
<Label DataContext="{Binding obj.field}">
<TextBlock Text="{Binding ., StringFormat=\{0\} suffix}" />
</Label>
Related question: I can't think of any reason why normal format strings that you can supply to string.Format() wouldn't work. They all should, both the standard and custom string. Here is a page with multiple examples. If there are any you find are not working, please provide examples.
Found a nice reference here with different examples : http://blog.stevex.net/string-formatting-in-csharp/