1

I have had this line in several location in my xaml for awhile StringFormat={}{0:#,##0.00} ISK}

Visual Studio has long since had it underlined but it always compiled fine. Today each one of these entries throws 3 errors one complaining type # doesn't exist, another that a period is expected for the , for the thousands separator, and finally that there is a space.

Is there a more reliable way to format this number as a string?

EDIT: For completion I undid the one change I made between the last build and the current one just to make sure that wasn't some how messing it up.

4
  • 1
    Have you tried StringFormat={}{0:n2} ISK}, it should produce roughly the same format. Commented Apr 1, 2017 at 4:44
  • Your format still works fine for me in VS2017. The editor's complaint (blue underline) even goes away once I build (of course it comes back once I make any change in the file). I have found the StringFormat binding attribute to always be finicky in this way, but it always compiles. If you're getting errors, sounds like you tripped over something new. You could move the formatting to some other place in the code (e.g. a converter that has a property for the format, or a text property in the view model that formats the numeric property of interest, that sort of thing). Commented Apr 1, 2017 at 4:56
  • Have you tried StringFormat='{}{0:#,##0.00} ISK'}? Commented Apr 1, 2017 at 5:35
  • @Mitch I just tested that and it works as well and removes the blue underline. Commented Apr 1, 2017 at 14:53

1 Answer 1

1

Is there a more reliable way to format this number as a string?

I haven't been able to reproduce your failure to compile the code. The XAML editor does complain, with the blue squiggly line and the complaint about the type not being found. If you don't mind the in-editor warning, I would expect that you should be able to get it to convert okay.

However, as a general replacement for the StringFormat binding property, you might consider a simple converter:

class StringFormatConverter : IValueConverter
{
    public string Format { get; set; }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        dynamic o = value;

        return o.ToString(Format);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

It will work with any type that has a ToString(string) method. You can use it like this:

<Window x:Class="TestSO43152859StringFormatNumber.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:l="clr-namespace:TestSO43152859StringFormatNumber"
        xmlns:s="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">

  <Window.DataContext>
    <s:Double>98123.45</s:Double>
  </Window.DataContext>

  <Window.Resources>
    <l:StringFormatConverter x:Key="iskNumericConverter" Format="#,##0.00 ISK"/>
  </Window.Resources>

  <Grid>
    <TextBlock HorizontalAlignment="Left" VerticalAlignment="Top"
               Text="{Binding Converter={StaticResource iskNumericConverter}}"/>
  </Grid>
</Window>
Sign up to request clarification or add additional context in comments.

1 Comment

I can't even replicate it myself now. I did not change a single line of code however now it compiles just fine. This however looks like a cleaner solution which hopefully will not give me problems in the future.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.