Show binded values in WPF without parsing - wpf

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.

Related

What is # in XAML StringFormat?

I am following the tutorial here. The example contains the line
<TextBlock Text="{Binding ElementName=wnd, Path=ActualWidth, StringFormat=Window width: {0:#,#.0}}" />
binding the window width to the textblock. If I remove "#,#", I notice I get the same result except there is no comma in the number if it's greater than 999. I see what it's doing. If I change it to
<TextBlock Text="{Binding ElementName=wnd, Path=ActualWidth, StringFormat=Window width: {0:##,#.0}}" />
I get the same thing. So my question is what does # mean exactly. Looked at MSDN and searched google but almost every example does not use any # signs.
# is a digit placeholder numeric format specifier in .NET that is replaced with the corresponding digit if one is present. Please refer to the docs for more information.
It has nothing to do with XAML really. You might as well use it programmatically when you for example call the ToString overload of a numeric type that accepts a format string.

Add leading 0 in the textbox

I have a textbox, the maxlength is 10. What I want is to add leading 0's to the text.
For example:
input 1, then 0000000001
input 12, then 0000000012
input 123, then 0000000123
I tried Padding a numeric display in WPF
It is not working and I don't want to use code behind text changed event. I'd prefer pure xaml.
UPDATED:
The code is simple. Something like
<TextBox Text="{Binding Info,StringFormat={}{0:0000000000}}" MaxLength="10" />
It shows 0000000000 in the first place, then you can't input the 11th letter to let the byte shift to the left afterwards.
Assume you binding a int type property to TextBox.Text, all you need to do is setting Binding.StringFormat correctly.
<TextBox Text={Binding path to you int property, StringFormat=d10}/>

format string for databinding, thousands separator and brackets without Currency sign

I want to databind a grid column in WPF so that the value looks like this for negative numbers
(1,234.00)
I can do Binding="{Binding Path=ContractBTM.BasisAmount,StringFormat={}{0:0.00;(0.00)}}" but that does not give me the separator.
I can do StringFormat=N but the negative values use the minus sign.
I tried using the #,### syntax but it doesn't like the comma in my binding string.
OK
I ended up going with StringFormat={}{0:#,##0.00;(#,##0.00)} and ignoring the errors
<TextBlock Text="{Binding Path=ContractBTM.BasisAmount, StringFormat={}{0:0.00;(0,000.00)}}" />

WPF RibbonTextBox databound to double

I use the default WPF ribbon shipped with VS2012 Express.
When RibbonTextBox databound to double property in viewmodel initialised by value 1.75, it displays it and allows modifying numbers around the decimal separator without framing it in red colour as it does when entered non-numeric character such as 'x' etc.
But once decimal separator deleted, there's no way to type it back into the RibbonTextBox. It accepts nonsense characters, but not the decimal separator. In other words, after deleting the decimal separator, it behaves rather as databound to int.
XAML
xmlns:rib="http://schemas.microsoft.com/winfx/2006/xaml/presentation/ribbon"
...
<StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch" >
<Label Content="Source Gamma " />
<rib:RibbonTextBox Text="{Binding SrcGamma, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="50"/>
</StackPanel>
C# code behind
public double SrcGamma { get; set; } // initialised to 1.75d
Note 1: First, I suspected it to be a culture problem. My Windows culture (cs-CZ) uses ',' decimal separator but my application displayed '.' separator.
a) Setting Windows culture separator to '.' didn't help.
b) After overrriding application locale according to this SO question, the correct Windows culture separator is displayed, but cannot be entered either.
Note 2:
During my investigation I added a standard TextBox (outside the ribbon). After adding a custom double validation rule to it, it started behaving just the same way as the RibbonTextBox mentioned.
Thanks in advance for any suggestion.
The answer was hidden in this SO Article.
The strange behaviour was caused by UpdateSourceTrigger=PropertyChanged I used to use in .NET 3.5 where it behaved in an expected way. After removing it, the decimal separator can be added without any problems now.
Well, MS had reasons to do this change, so let's bear it in mind.

Silverlight Stringformat : malformed string returns empty string instead of exception

Here's my code snippet :
<TextBox Text="{Binding Path=Amount, Mode=TwoWay, StringFormat=\{0:N\}}" />
If the user enters letters or a large number etc, the stringformat dies silently. How can i raise an exception instead ?
Thanks
Bindings swallow exceptions thrown when text input cannot be converted to the data type required by the property on source object. However you can specify ValidatesOnException in the binding. That will cause the standard red border reporting of a conversion problem. BTW this is unrelated to the string format property which is only relevant for displaying the current value, it isn't in play when user is entering data.
<TextBox Text="{Binding Path=Amount, Mode=TwoWay, StringFormat=\{0:N\}, ValidatesOnExceptions=True}" HorizontalAlignment="Left" Width="200"/>
Note I've limited the width and aligned to left. One of the problems with the default validation popup is that is that it is always displayed to the right, which is a bit of a problem when the text box right border is flush with the right edge of the silverlight control's right edge.
Have you thought of writing a filter behavior that allows you to control exactly what goes into the text box?

Resources