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?

4
  • 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 Commented May 17, 2015 at 13:22
  • try to debug the converter and see what's inside value. Commented May 17, 2015 at 13:22
  • @CharlesMager ah yes, thank you. Only in the question though, not the actual code I was running :) Commented May 17, 2015 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. Commented May 19, 2015 at 7:33

1 Answer 1

-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.

Sign up to request clarification or add additional context in comments.

Comments

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.