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.
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:
Now, bind the view to this
SizeFormattedproperty: