Skip to content

Instantly share code, notes, and snippets.

@thoemmi
Last active January 21, 2017 13:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thoemmi/03b81c07586f63accb83521d783d6749 to your computer and use it in GitHub Desktop.
Save thoemmi/03b81c07586f63accb83521d783d6749 to your computer and use it in GitHub Desktop.
Presenting Byte Size Values in WPF, see https://thomasfreudenberg.com/archive/2017/01/21/presenting-byte-size-values-in-wpf/ for description
public class FormatKbSizeConverter : IValueConverter {
[DllImport("shlwapi.dll", CharSet = CharSet.Unicode)]
private static extern long StrFormatByteSizeW(long qdw, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszBuf,
int cchBuf);
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
var number = System.Convert.ToInt64(value);
var sb = new StringBuilder(32);
StrFormatByteSizeW(number, sb, sb.Capacity);
return sb.ToString();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
return DependencyProperty.UnsetValue;
}
}
<Window
x:Class="FormatByteSizeConverterDemo.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:local="clr-namespace:FormatByteSizeConverterDemo"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:system="clr-namespace:System;assembly=mscorlib"
Title="MainWindow"
Width="525"
Height="350"
mc:Ignorable="d">
<Grid>
<Grid.Resources>
<!-- array of numbers -->
<x:Array x:Key="numbers" Type="system:Int64">
<system:Int64>-1</system:Int64>
<system:Int64>0</system:Int64>
<system:Int64>1</system:Int64>
<system:Int64>2</system:Int64>
<system:Int64>532</system:Int64>
<system:Int64>1340</system:Int64>
<system:Int64>23506</system:Int64>
<system:Int64>2400016</system:Int64>
<system:Int64>2400000000</system:Int64>
<x:Static Member="system:Int64.MaxValue" />
</x:Array>
<!-- the actual converter -->
<local:FormatKbSizeConverter x:Key="FormatKbSizeConverter" />
<!-- style to reght-align the values in the DataGrid -->
<Style x:Key="RightCell" TargetType="{x:Type TextBlock}">
<Setter Property="TextAlignment" Value="Right" />
<Setter Property="HorizontalAlignment" Value="Stretch" />
</Style>
</Grid.Resources>
<DataGrid
AutoGenerateColumns="False"
IsReadOnly="True"
ItemsSource="{StaticResource numbers}">
<DataGrid.Resources>
<!-- the actual converter -->
<local:FormatKbSizeConverter x:Key="FormatKbSizeConverter" />
</DataGrid.Resources>
<DataGrid.Columns>
<!-- First column shows the plain values -->
<DataGridTextColumn
Binding="{Binding}"
ElementStyle="{StaticResource RightCell}"
Header="Plain" />
<!-- Second column shows the formatted values -->
<DataGridTextColumn
Binding="{Binding Converter={StaticResource FormatKbSizeConverter}}"
ElementStyle="{StaticResource RightCell}"
Header="Formatted" />
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment