IO2BinConverter.cs 1.13 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

namespace Flyad7_WPF.Converters
{
    public class IO2BinConverter : IValueConverter
    {
        #region IValueConverter 成员

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            UInt16 io = (UInt16)value;
            string str = "";
            for (int i = 0; i < 4; i++) {
                byte b = (byte)((io >> (i * 4)) & 0x0f);
                string str_b = System.Convert.ToString(b, 2);
                if (string.IsNullOrEmpty(str))
                {
                    str = str_b.PadLeft(4, '0');
                }
                else {
                    str = str_b.PadLeft(4, '0') + " " + str;
                }
            }
            return str;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return System.Convert.ToUInt16((string)value, 2);
        }

        #endregion
    }
}