4

In Microsoft Blend for Visual Studio (Express 2013 and Community 2015 RC), I have created some sample data with a collection of items consisting of a number and a string.

Data sample

The number amount is bound to a TextBlock:

<TextBlock Text="{Binding amount}" />

The numbers displays fine, except I want them formatted to a string which displays 2 decimals. Since StringFormat is not available for Universal Apps, I tried adding a converter which attempts to achieve the same thing:

public class StringFormatConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        return string.Format(parameter as string, value);
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        return null;
    }
}

I add a resource for it:

<Page.Resources>
    <ResourceDictionary>
        <local:StringFormatConverter x:Name="StringFormat"/>
    </ResourceDictionary>
</Page.Resources>

I adjust my TextBlock's binding:

<TextBlock Text="{
    Binding amount, 
    Converter={StaticResource StringFormat}, 
    ConverterParameter='{}{0:f2}'}" />

But when I do this, the view panel displays System.Object for each amount.

Even when I add a converter that does nothing and just returns the value or the toString() of the value, I get the same result! This only happens with the sample data type Number.

How can I format the binding to a number from a sample data set in Blend?

Community
  • 1
  • 1
Bas Peeters
  • 3,269
  • 4
  • 33
  • 49
  • Though it probably won't fix if even returning a fixed value doesn't work, there's a typo in your converter parameter - `{}{0:f2}}` should be `{}{0:f2}`. As it is, it will throw an exception – Charles Mager May 17 '15 at 13:22
  • try to debug the converter and see what's inside value. – CKII May 17 '15 at 13:22
  • @CharlesMager ah yes, thank you. Only in the question though, not the actual code I was running :) – Bas Peeters May 17 '15 at 13:43
  • @CKII I don't think I can debug the converter during design time, or can I? I'm not running the application, I'm just designing it graphically (or rather, XAMLically) during which the converter fails and defaults to `System.Object`. – Bas Peeters May 19 '15 at 07:33

1 Answers1

-1

Not sure how helpful this is going to be after all these years, but why don't you do it in code behind? You just have to update the "Text" property of the TextBlock after the necessary conversion is done.