Combine text to String.Format - wpf

I have this StringFormat StringFormat={}{0:#,0}} that represent decimal numbers and I want add text of my own after this number.
This is what I have tried (but it does not compile with the text inside the StringFormat)
Content="{Binding Path=(my:MyClass.MyStaticProperty),StringFormat={}{0:#,0}} My Text"
my:MyClass.MyStaticProperty is integer type.

As you use Label, Content cannot be formatted with StringFormat. Please view this topic.
Your solution is to use ContentStringFormat:
<Label Content="{Binding Source={x:Static my:MyClass.MyStaticProperty}}"
ContentStringFormat="{}{0:#.0} My Text"/>

Related

Why is AccessText not used when using ContentStringFormat?

When using a string value as Content of a ContentControl (like Label), using an _ inside the string allows using the following letter as access key, like this:
<Label Content="Text with _access key"/>
or even
<Label Content="{Binding Text}"/>
if Text is a string property containing an _.
However, when using ContentStringFormat which contains the _, it doesn't work any more:
<Label Content="{Binding Value}" ContentStringFormat="_Formatted value {0}"/>
I have seen in the debugger that no AccessText is used in this case.
As a workaround, I have used AccessText explicitly:
<Label>
<AccessText Text="{Binding Value, StringFormat=_Formatted value {0}"/>
</Label>
It works that way but I still want to know why it doesn't when using ContentStringFormat.
Maybe This topic will give you some insight about the AccesText.

Label % suffix in binding

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

What's the XAML equivalent of .toFixed(2)?

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>

Custom DateTime stringformat in WPF

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

Silverlight TextBlock Text Attribute vs Content

When would I use the Text attribute of the <TextBlock> and when should I put my text in the content of the <TextBlock>?
<TextBlock Text="Example Text" />
vs.
<TextBlock>Example Text</TextBlock>
The former can be bound, whilst the latter is particularly useful when combining Runs:
<TextBlock Text="{Binding SomeProperty}"/>
<TextBlock>
<Run>You have </Run>
<Run Text="{Binding Count}"/>
<Run>items.</Run>
</TextBlock>
The use of the Text property has become common as a result of previous versions of the Xaml parser but the placing the text as content is more natural especially if you have a background in HTML.
The fact the many TextBlocks either have simple short chunks of literal text in or are bound. Would tip the balance IMO to using the Text property. In addition any globalisation that may come along latter may end with those literals being replaced by bindings as well.

Resources