CheckListBoxEditorControl.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows;
  5. using System.Windows.Media;
  6. using InABox.Core;
  7. using Syncfusion.Windows.Shared;
  8. using Syncfusion.Windows.Tools.Controls;
  9. namespace InABox.DynamicGrid
  10. {
  11. public class CheckListBoxEditorControl : DynamicEditorControl<Dictionary<string, bool>>, ILookupEditorControl
  12. {
  13. private SolidColorBrush _color;
  14. private Dictionary<object, string> _lookups;
  15. private Type _realtype;
  16. private Dictionary<string, bool> _value;
  17. private bool bLoading;
  18. private CheckListBox Editor;
  19. public override void Configure()
  20. {
  21. base.Configure();
  22. Editor.IsEnabled = false;
  23. Editor.Background = Brushes.WhiteSmoke;
  24. OnDefineLookups?.Invoke(this);
  25. }
  26. public override int DesiredHeight()
  27. {
  28. return 300;
  29. }
  30. public override void SetFocus()
  31. {
  32. Editor.Focus();
  33. }
  34. public Dictionary<string, string> OtherColumns { get; set; }
  35. public event OnDefineLookup OnDefineLookups;
  36. public event OnLookupsDefined OnLookupsDefined;
  37. public void LoadLookups(CoreTable otherfields)
  38. {
  39. _lookups = new Dictionary<object, string>();
  40. foreach (var row in otherfields.Rows)
  41. _lookups[row[ColumnName]] = row["Display"].ToString();
  42. SetupCheckListBox();
  43. OnLookupsDefined(this);
  44. }
  45. protected override FrameworkElement CreateEditor()
  46. {
  47. MinHeight = 50;
  48. Editor = new CheckListBox
  49. {
  50. VerticalAlignment = VerticalAlignment.Stretch,
  51. VerticalContentAlignment = VerticalAlignment.Top,
  52. HorizontalAlignment = HorizontalAlignment.Stretch,
  53. //SelectedItemBackground = Brushes.Transparent,
  54. //Foreground = Brushes.Black
  55. DisplayMemberPath = "Value",
  56. SelectedValuePath = "Key"
  57. };
  58. SkinStorage.SetVisualStyle(Editor, "Metro");
  59. Editor.SelectionChanged += (o, e) =>
  60. {
  61. if (!bLoading)
  62. CheckChanged();
  63. };
  64. return Editor;
  65. }
  66. public override int DesiredWidth()
  67. {
  68. return 150;
  69. }
  70. protected override Dictionary<string, bool> RetrieveValue()
  71. {
  72. _value = Activator.CreateInstance(_realtype) as Dictionary<string, bool>;
  73. foreach (KeyValuePair<object, string> pair in Editor.Items)
  74. _value[pair.Key.ToString()] = Editor.SelectedItems.Contains(pair);
  75. return _value;
  76. }
  77. protected override void UpdateValue(Dictionary<string, bool> value)
  78. {
  79. _realtype = value.GetType();
  80. _value = value;
  81. SetupCheckListBox();
  82. }
  83. public override void SetColor(Color color)
  84. {
  85. if (Editor.IsEnabled)
  86. Editor.Background = new SolidColorBrush(color);
  87. else
  88. _color = new SolidColorBrush(color);
  89. }
  90. private void SetupCheckListBox()
  91. {
  92. if (_lookups == null || _value == null || bLoading)
  93. return;
  94. bLoading = true;
  95. var sorted = _lookups.ToList();
  96. sorted.Sort((pair1, pair2) => pair1.Value.ToString().CompareTo(pair2.Value.ToString()));
  97. Editor.ItemsSource = sorted;
  98. if (_value != null)
  99. {
  100. foreach (var pair in sorted)
  101. {
  102. var key = pair.Key.ToString();
  103. if (_value.ContainsKey(key))
  104. {
  105. if (_value[key])
  106. //Logger.Send(LogType.Information, "", String.Format("- Enabled: {0} / {1}", key, pair.Value));
  107. Editor.SelectedItems.Add(pair);
  108. //else
  109. //Logger.Send(LogType.Information, "", String.Format("- Disabled: {0} / {1}", key, pair.Value));
  110. }
  111. else
  112. {
  113. //Logger.Send(LogType.Information, "",String.Format("- New Token: {0} / {1}",key,pair.Value));
  114. Editor.SelectedItems.Add(pair);
  115. }
  116. }
  117. Editor.IsEnabled = true;
  118. if (_color != null)
  119. Editor.Background = _color;
  120. }
  121. bLoading = false;
  122. }
  123. }
  124. }