2026-04-09 17:06:29 +08:00
|
|
|
using HandyControl.Controls;
|
2025-10-10 13:22:56 +08:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Windows;
|
2026-04-09 17:06:29 +08:00
|
|
|
using System.Windows.Controls;
|
|
|
|
|
using System.Windows.Data;
|
2025-10-10 13:22:56 +08:00
|
|
|
|
|
|
|
|
namespace Laservall.Solidworks.Common
|
|
|
|
|
{
|
2026-04-09 17:06:24 +08:00
|
|
|
public static class ComboBoxDataRegistry
|
|
|
|
|
{
|
|
|
|
|
private static readonly Dictionary<string, List<string>> _data =
|
|
|
|
|
new Dictionary<string, List<string>>(StringComparer.OrdinalIgnoreCase);
|
|
|
|
|
|
|
|
|
|
private static readonly List<string> _defaultItems = new List<string>();
|
|
|
|
|
|
|
|
|
|
public static void Register(string propertyName, List<string> items)
|
|
|
|
|
{
|
|
|
|
|
_data[propertyName] = items ?? new List<string>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static List<string> GetItems(string propertyName)
|
|
|
|
|
{
|
|
|
|
|
List<string> items;
|
|
|
|
|
if (_data.TryGetValue(propertyName, out items))
|
|
|
|
|
{
|
|
|
|
|
return items;
|
|
|
|
|
}
|
|
|
|
|
return _defaultItems;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-10 13:22:56 +08:00
|
|
|
public class ComboxPropertyEditor : PropertyEditorBase
|
|
|
|
|
{
|
|
|
|
|
public override FrameworkElement CreateElement(PropertyItem propertyItem)
|
|
|
|
|
{
|
2026-04-09 17:06:29 +08:00
|
|
|
var comboBox = new System.Windows.Controls.ComboBox();
|
2026-04-09 17:06:24 +08:00
|
|
|
comboBox.IsEditable = true;
|
|
|
|
|
var items = ComboBoxDataRegistry.GetItems(propertyItem.DisplayName);
|
|
|
|
|
comboBox.ItemsSource = items;
|
2025-10-10 13:22:56 +08:00
|
|
|
comboBox.SelectedItem = propertyItem.Value?.ToString();
|
|
|
|
|
comboBox.SelectionChanged += (s, e) =>
|
|
|
|
|
{
|
|
|
|
|
propertyItem.Value = comboBox.SelectedItem;
|
|
|
|
|
};
|
|
|
|
|
return comboBox;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override DependencyProperty GetDependencyProperty()
|
|
|
|
|
{
|
2026-04-09 17:06:29 +08:00
|
|
|
return System.Windows.Controls.ComboBox.SelectedItemProperty;
|
2025-10-10 13:22:56 +08:00
|
|
|
}
|
|
|
|
|
}
|
2026-03-20 09:33:27 +08:00
|
|
|
|
|
|
|
|
public class CategoryComboxPropertyEditor : PropertyEditorBase
|
|
|
|
|
{
|
2026-04-09 17:06:29 +08:00
|
|
|
private static List<PartTypeItem> _allPartTypes = new List<PartTypeItem>();
|
|
|
|
|
|
|
|
|
|
public static void SetPartTypes(List<PartTypeItem> items)
|
|
|
|
|
{
|
|
|
|
|
_allPartTypes = items ?? new List<PartTypeItem>();
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-20 09:33:27 +08:00
|
|
|
public override FrameworkElement CreateElement(PropertyItem propertyItem)
|
|
|
|
|
{
|
2026-04-09 17:06:29 +08:00
|
|
|
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;
|
|
|
|
|
|
2026-03-20 09:33:27 +08:00
|
|
|
comboBox.SelectionChanged += (s, e) =>
|
|
|
|
|
{
|
2026-04-09 17:06:29 +08:00
|
|
|
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;
|
|
|
|
|
};
|
2026-03-20 09:33:27 +08:00
|
|
|
};
|
2026-04-09 17:06:29 +08:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-03-20 09:33:27 +08:00
|
|
|
return comboBox;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override DependencyProperty GetDependencyProperty()
|
|
|
|
|
{
|
2026-04-09 17:06:29 +08:00
|
|
|
return System.Windows.Controls.ComboBox.SelectedValueProperty;
|
2026-03-20 09:33:27 +08:00
|
|
|
}
|
|
|
|
|
}
|
2026-04-09 17:06:24 +08:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-10-10 13:22:56 +08:00
|
|
|
}
|