Binding to show double as int - wpf

I want to to show the duration in minutes on my control but I don't want it to show up as with decimal numbers (eg, 65 instead of 65.94503).
<TextBlock Text="{Binding Duration.TotalMinutes, StringFormat=\{0\} minutes}" />
How can I do it?

If you want an integer to display, I think that all you need to do is set the StringFormat property as follows:
<TextBlock Text="{Binding Duration.TotalMinutes, StringFormat=N0}"/>

Do that in code instead of the markup. You can unit test that easily. Make TotalMinutes a string or int instead and format it in the ViewModel.

Related

Show binded values in WPF without parsing

I bind single properties to a TextBox without a specific StringFormat with the intension to show the value exactly like the user entered it.
<TextBox x:Name="TBSingleValue" Text="{Binding Path=SingleValue, ValidatesOnDataErrors=True}" />
The current automatic StringFormat creates the following result:
123456789 => 1.234568E+08
0.123456789 => 0.1234568
I need to display the values within -10e-9 and 10e9 without the scientific formating or automatic round.
With a StringFormat:
<TextBox x:Name="TBSingleValue" Text="{Binding Path=SingleValue, ValidatesOnDataErrors=True, StringFormat={}{0:F9}}" />
it parsed as expected but now I get every Decimal place, even when the value doesn't need one.
4 => 4.000000000
So here my Question:
Do you know what kind of StringFormat I should use to display every enteres Decimal place without showing unnessessary ones?
Best case would also a support of , and . as Decimal delimiter.
Edit:
I came up with a quite simple solution:
<TextBox x:Name="TBSingleValue" Text="{Binding Path=SingleValue, ValidatesOnDataErrors=True, StringFormat={}{0:0.###########}}" />
The custom StringFormat character # shows a digit only if one is present. The 0 at the beginning ensures that there is a 0 at the beginning even for numbers of 0.001.
This structure disables the scientific notation and gives the number with the required precision, without unnecessary 0 at the end.
Even -0.00000001 is displayed correct.

Combine text to String.Format

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

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

WPF: Textbox Binding with StringFormat={}{0:F2}. Don't show zero's

I am binding an object to a TextBox with the following XAML:
<TextBox Name="MyTextBox" Text="{Binding Path=MyValue, Mode=TwoWay, StringFormat={}{0:F2}}" />
Naturally when I bind a new object (which values are all still zero) the Text property is set to 0.00. I have several of these TextBoxes, which makes it tedious to delete every value before entering a new one.
At the moment I'm clearing these boxes in the Window_Loaded method using the FindVisualChildren method.
It just feels clunky though. Is there a neat way of doing this?
Try the following:
StringFormat={}{0:#.##}
It will format to two decimal places and won't show zeroes.

How to set WPF string format as percent wihout multiplying by 100?

I have a textbox bound to a property in an object.
I have setup the string format to be p0.
However, when I enter 12 for example it is formatted as 1200% (multiplies by 100 and add % sign)
How can i set the stringformat so that for exampe 20 is formatted as 20% ?
My current control is :
<TextBox Text="{Binding Path=MyCase, ValidatesOnDataErrors=True, ValidatesOnExceptions=True, StringFormat=p0}"/>
how t change the string format so that the format for 7 is 7% not 700% ?
"{Binding Path=Percentage, StringFormat={}{0}%}"
Another solution is to wrap the % in single quotes, and put it inside the curley brackets:
<TextBlock Text="{Binding Percentage, StringFormat={}{0:#0.00'%'}}"/>

Resources