StringFormat in XAML

I am trying to format my string to have a comma every 3 places, and decimal if it is not an integer. I checked about 20 examples, and this is the closest that I came:

 <TextBlock x:Name="countTextBlock" Text="{Binding Count, StringFormat={0:n}}" /> 

But I get the error The property 'StringFormat' was not found in type 'Binding'. .

Any ideas what is wrong here? Windows Phone 8.1 seems to be different from WPF because all WPF resources say that it does.

( string constantly updated, so I need the code to be in XAML . I also need to stay connected. Unless, of course, I can have my own cake and eat it too.)

+6
source share
1 answer

It seems that, like Binding in WinRT, Binding in Windows Phone Universal Apps does not have the StringFormat property. One possible way around this limitation is to use Converter , as described in this blog post ,

To summarize the message, you can create an IValueConverter implantation that takes a string format as a parameter:

 public sealed class StringFormatConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, string language) { if (value == null) return null; if (parameter == null) return value; return string.Format((string)parameter, value); } public object ConvertBack(object value, Type targetType, object parameter, string language) { throw new NotImplementedException(); } } 

Create the resource of the above converter in your XAML, then you can use it, for example, as follows:

 <TextBlock x:Name="countTextBlock" Text="{Binding Count, Converter={StaticResource StringFormatConverter}, ConverterParameter='{}{0:n}'}" /> 
+10
source

Source: https://habr.com/ru/post/972874/


All Articles