BoolToStringConverter.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows.Data;
  8. namespace FWindSoft.Wpf.Converter
  9. {
  10. public class BoolToStringConverter : IValueConverter
  11. {
  12. private Dictionary<bool, string> m_DicItems;
  13. public BoolToStringConverter(Dictionary<bool, string> dicItems)
  14. {
  15. m_DicItems = dicItems ?? new Dictionary<bool, string>();
  16. }
  17. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  18. {
  19. if (value.GetType() != typeof(bool))
  20. return string.Empty;
  21. string result = string.Empty;
  22. if (m_DicItems.TryGetValue((bool) value, out result))
  23. {
  24. return result;
  25. }
  26. return value.ToString();
  27. }
  28. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  29. {
  30. if (targetType != typeof(bool))
  31. return false;
  32. foreach (var mDicItem in m_DicItems)
  33. {
  34. if (mDicItem.Value == value.ToString())
  35. return mDicItem.Key;
  36. }
  37. return false;
  38. }
  39. }
  40. }