Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8857523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:36:37+00:00 2026-06-14T14:36:37+00:00

just having some trouble formatting the displayed string when databinding. Suppose I have a

  • 0

just having some trouble formatting the displayed string when databinding.

Suppose I have a property Size:

    /// <summary>
    /// Size information
    /// </summary>
    private long _size;
    public long Size
    {
        get
        {
            return _size;
        }
        set
        {
            if (value != _size)
            {
                _size = value;
                NotifyPropertyChanged("Size");
            }
        }
    }

And this size is an integer representing a number of bytes. I want to display a value representing the size depending on how large the integer is. For example:

Size = 1 byte
Size = 1 kilobyte
Size = 100 megabytes

Here is my XAML for the TextBlock:

<TextBlock Text="{Binding Size, StringFormat='Size: {0}'}" TextWrapping="Wrap" Margin="12,110,0,0" Style="{StaticResource PhoneTextSubtleStyle}" Visibility="{Binding Visible}" FontSize="14" Height="20" VerticalAlignment="Top" HorizontalAlignment="Left" Width="200"/>

As of right now it just shows “Size: 50” meaning 50 bytes, but I want it to show “Size: 50 bytes/kilobytes/megabytes” (whichever one is appropriate) otherwise i’ll get “Size: 50000000000000” and huge numbers like that.

How would I go about ‘dynamically’ changing the stringformat?

Keep in mind the textblock is encased within a LongListSelector bounded by an ObservableCollection so simply getting the textblock and changing the text won’t work since there will be heaps of objects using the textblock’s format, if you know what I mean.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T14:36:38+00:00Added an answer on June 14, 2026 at 2:36 pm

    In my case, I’ve used a bit of a hack. I’m binding the view to a viewModel which contains an additional string property that contains the formatted value. For example, something like this:

    //using short scale: http://en.wikipedia.org/wiki/Long_and_short_scales#Comparison
    const decimal HundredThousand = 100 * 1000;
    const decimal Million = HundredThousand * 10;
    const decimal Billion = Million * 1000; //short scale
    const decimal Trillion = Billion * 1000; //short scale
    
    const string NumberFormatKilo = "{0:##,#,.## K;- ##,#,.## K;0}";
    const string NumberFormatMillion = "{0:##,#,,.## M;- ##,#,,.## M;0}";
    const string NumberFormatBillion = "{0:##,#,,,.## B;- ##,#,,,.## B;0}";
    const string NumberFormatTrillion = "{0:##,#,,,,.## T;- ##,#,,,,.## T;0}";
    
    public decimal Size 
    {
        get; set;
    }
    
    public string SizeFormatted 
    {
        get 
        {
            var format = GetUpdatedNumberFormat(Size);
            return string.Format(format, Size);
        }
    }
    
    private static string GetUpdatedNumberFormat(decimal value)
    {
        string format = NumberFormat;
        if (Math.Abs(value) >= Constants.Trillion)
            format = NumberFormatTrillion;
        else if (Math.Abs(value) >= Constants.Billion)
            format = NumberFormatBillion;
        else if (Math.Abs(value) >= Constants.Million)
            format = NumberFormatMillion;
        else if (Math.Abs(value) >= Constants.HundredThousand)
            format = NumberFormatKilo;
        return format;
    }
    

    Now, bind the view to this SizeFormatted property:

    <TextBlock Text="{Binding SizeFormatted}" ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been having some trouble finding a good way to output just the
I'm having some trouble writing a query. For example: Let's say I have just
I'm having some trouble with the CreateDate() function, it's just erroring and I have
I'm just learning how to use iterators over vectors, and I'm having some trouble
I have just started to play with Play framework (2.0) and I'm having some
I'm having some trouble. I just discovered that you can control vimeo with js,
I'm having some trouble using sessions with php.. I think I have figured out
I just started working with CodeIgniter and I am having some trouble with the
I've just started learning Linux and I'm having some trouble disabling GCC's optimization for
I'm having some trouble with IE6 and jQuery UI. I have a popup dialog

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.