using HandyControl.Controls; using System; using System.Collections.Generic; using System.Linq; using System.Windows; using System.Windows.Controls; using System.Windows.Data; namespace Laservall.Solidworks.Common { public static class ComboBoxDataRegistry { private static readonly Dictionary> _data = new Dictionary>(StringComparer.OrdinalIgnoreCase); private static readonly List _defaultItems = new List(); public static void Register(string propertyName, List items) { _data[propertyName] = items ?? new List(); } public static List GetItems(string propertyName) { List items; if (_data.TryGetValue(propertyName, out items)) { return items; } return _defaultItems; } } public class ComboxPropertyEditor : PropertyEditorBase { public override FrameworkElement CreateElement(PropertyItem propertyItem) { var comboBox = new System.Windows.Controls.ComboBox(); comboBox.IsEditable = true; var items = ComboBoxDataRegistry.GetItems(propertyItem.DisplayName); comboBox.ItemsSource = items; comboBox.SelectedItem = propertyItem.Value?.ToString(); comboBox.SelectionChanged += (s, e) => { propertyItem.Value = comboBox.SelectedItem; }; return comboBox; } public override DependencyProperty GetDependencyProperty() { return System.Windows.Controls.ComboBox.SelectedItemProperty; } } public class CategoryComboxPropertyEditor : PropertyEditorBase { private static List _allPartTypes = new List(); public static void SetPartTypes(List items) { _allPartTypes = items ?? new List(); } public override FrameworkElement CreateElement(PropertyItem propertyItem) { var comboBox = new System.Windows.Controls.ComboBox { IsEditable = true, IsTextSearchEnabled = false }; var allItems = _allPartTypes; comboBox.ItemsSource = allItems; comboBox.DisplayMemberPath = "DisplayText"; comboBox.SelectedValuePath = "Code"; var currentCode = propertyItem.Value?.ToString(); if (!string.IsNullOrEmpty(currentCode)) { var match = allItems.FirstOrDefault(x => string.Equals(x.Code, currentCode, StringComparison.OrdinalIgnoreCase)); if (match != null) { comboBox.SelectedItem = match; } } bool suppressFilter = false; comboBox.SelectionChanged += (s, e) => { if (comboBox.SelectedItem is PartTypeItem selected) { propertyItem.Value = selected.Code; } }; comboBox.Loaded += (s, e) => { var textBox = comboBox.Template.FindName("PART_EditableTextBox", comboBox) as TextBox; if (textBox == null) return; textBox.TextChanged += (s2, e2) => { if (suppressFilter) return; var text = textBox.Text; if (string.IsNullOrEmpty(text)) { comboBox.ItemsSource = allItems; } else { var lower = text.ToLowerInvariant(); var filtered = allItems.Where(x => (x.Code != null && x.Code.ToLowerInvariant().Contains(lower)) || (x.TypeName != null && x.TypeName.ToLowerInvariant().Contains(lower)) ).ToList(); comboBox.ItemsSource = filtered; } suppressFilter = true; comboBox.IsDropDownOpen = true; textBox.Text = text; textBox.CaretIndex = text.Length; suppressFilter = false; }; }; comboBox.DropDownClosed += (s, e) => { if (comboBox.SelectedItem is PartTypeItem selected) { suppressFilter = true; var textBox = comboBox.Template.FindName("PART_EditableTextBox", comboBox) as TextBox; if (textBox != null) { textBox.Text = selected.DisplayText; textBox.CaretIndex = textBox.Text.Length; } suppressFilter = false; comboBox.ItemsSource = allItems; } }; return comboBox; } public override DependencyProperty GetDependencyProperty() { return System.Windows.Controls.ComboBox.SelectedValueProperty; } } public class ReadOnlyTextPropertyEditor : PropertyEditorBase { public override FrameworkElement CreateElement(PropertyItem propertyItem) { return new System.Windows.Controls.TextBlock { VerticalAlignment = VerticalAlignment.Center }; } public override DependencyProperty GetDependencyProperty() { return System.Windows.Controls.TextBlock.TextProperty; } } }