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.
Related
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.
I am having an issue with NumberTextBox. Below is the xaml code.
<cc:NumberTextBox
Margin="5,0,5,0"
HorizontalAlignment="Stretch"
IsEnabled="{Binding IsEditable}"
Style="{StaticResource TextBox_Default}"
TabIndex="25"
IsDecimalAllowed="True"
Text="{Binding ProductQuantity, ValidatesOnDataErrors=True, StringFormat={}{0:#.##}, UpdateSourceTrigger=PropertyChanged}" />
It should allow user to enter only 2 digits after the decimal. I tried to use StringFormat but it i snot working.
Example: 23.65567 it should allow user to enter only 23.65
Any suggestions to fix this issue.
Thanks in advance.
First of all, NumberTextBox is not a standard part of WPF. you should tag whatever library you are using for that element. then you will find more help here.
This link goes to a page where you can write your own NumberTextBox. As for digit limiting, just test for the "dot" and allow two more key-downs.
This link is an open source collection for numeric input control
I have a nullable field in the database, called Generation. It specifies things like "Jr.", "II" and so on. I want a mean of conditionally specifying the generation of the client, if it isn't null, otherwise not displaying it at all. I thought that the following would work:
<TextBlock Text="{Binding LastName}" />
<TextBlock Text="{Binding Generation, StringFormat= {0}}" />
<TextBlock Text=", " />
However I'm getting an error saying that "0 is not supported in a Windows Presentation Foundation (WPF) project". The field value Generation is a varchar field. Can I do what I want with the StringFormat attribute of the TextBlock class, or do I need to use a converter?
You need to write this as:
<TextBlock Text="{Binding Generation, StringFormat={}{0}}" />
This is due to the nature of using the Markup Extension, which gives special meaning to {}. By adding this at the beginning (when you have no text before your first format specifier), it has the effect of "escaping" the string format specification for you, similar to how # is used to handle string literals in C#.
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?
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/