Laservall_solidworks_inject/Common/PropertyEditor.cs

174 lines
5.7 KiB
C#
Raw Normal View History

using HandyControl.Controls;
2025-10-10 13:22:56 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
2025-10-10 13:22:56 +08:00
namespace Laservall.Solidworks.Common
{
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)
{
var comboBox = new System.Windows.Controls.ComboBox();
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()
{
return System.Windows.Controls.ComboBox.SelectedItemProperty;
2025-10-10 13:22:56 +08:00
}
}
public class CategoryComboxPropertyEditor : PropertyEditorBase
{
private static List<PartTypeItem> _allPartTypes = new List<PartTypeItem>();
public static void SetPartTypes(List<PartTypeItem> items)
{
_allPartTypes = items ?? new List<PartTypeItem>();
}
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;
}
}
2025-10-10 13:22:56 +08:00
}