BytesConverter
public class BytesConverter : IValueConverter { public bool IsSpeed { get; set; } public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { double kb = 1024.00; double mb = 1024.00 * 1024; double gb = 1024.00 * 1024 * 1024; double bytes = value == null ? 0 : (long)(value); string result = null; if (bytes < kb) result = (bytes / 1024.00).ToString("N2") + "B"; else if (bytes >= kb && bytes < mb) result = (bytes / kb).ToString("N2") + "KB"; else if (bytes >= mb && bytes < gb) result = (bytes / mb).ToString("N2") + "MB"; else result = bytes / gb + "GB"; if (IsSpeed) result += "/s"; return result; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { return System.Windows.Data.Binding.DoNothing; } }
食用方法:
<converter:BytesConverter x:Key="BytesConverter" IsSpeed="False" />