140 lines
4.3 KiB
C#
140 lines
4.3 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Collections.ObjectModel;
|
||
using System.ComponentModel;
|
||
using System.Globalization;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using System.Windows;
|
||
using System.Windows.Controls;
|
||
using System.Windows.Data;
|
||
using System.Windows.Documents;
|
||
using System.Windows.Input;
|
||
using System.Windows.Media;
|
||
using System.Windows.Media.Imaging;
|
||
using System.Windows.Shapes;
|
||
|
||
using Microsoft.Win32;
|
||
|
||
using Melskin.Assets;
|
||
|
||
namespace MelskinTest;
|
||
/// <summary>
|
||
/// IconsWindow.xaml 的交互逻辑
|
||
/// </summary>
|
||
public partial class IconsWindow : INotifyPropertyChanged
|
||
{
|
||
// 1. 一个私有的、包含所有图标的原始数据集合
|
||
private readonly ObservableCollection<MaterialSymbol> _allSymbols = new ObservableCollection<MaterialSymbol>();
|
||
|
||
// 2. 一个公开的 ICollectionView,UI 将绑定到这个视图
|
||
public ICollectionView SymbolCollectionView { get; }
|
||
|
||
private string _selectedSymbolName;
|
||
public string SelectedSymbolName
|
||
{
|
||
get => _selectedSymbolName;
|
||
set { _selectedSymbolName = value; OnPropertyChanged(nameof(SelectedSymbolName)); }
|
||
}
|
||
|
||
private string _searchText = string.Empty;
|
||
// 3. 绑定到搜索框的属性
|
||
public string SearchText
|
||
{
|
||
get => _searchText;
|
||
set
|
||
{
|
||
if (_searchText == value) return;
|
||
_searchText = value;
|
||
OnPropertyChanged(nameof(SearchText));
|
||
|
||
// 关键点: 当搜索文本改变时,刷新视图以重新应用筛选
|
||
SymbolCollectionView.Refresh();
|
||
}
|
||
}
|
||
|
||
public IconsWindow()
|
||
{
|
||
InitializeComponent();
|
||
|
||
// 从枚举加载所有图标到原始集合
|
||
LoadSymbols();
|
||
|
||
// 4. 初始化视图
|
||
SymbolCollectionView = CollectionViewSource.GetDefaultView(_allSymbols);
|
||
|
||
// 5. 为视图指定筛选逻辑
|
||
SymbolCollectionView.Filter = FilterSymbols;
|
||
|
||
this.DataContext = this;
|
||
}
|
||
|
||
private void LoadSymbols()
|
||
{
|
||
var symbols = Enum.GetValues(typeof(MaterialSymbol)).Cast<MaterialSymbol>();
|
||
_allSymbols.Clear();
|
||
foreach (var symbol in symbols)
|
||
{
|
||
_allSymbols.Add(symbol);
|
||
}
|
||
}
|
||
|
||
// 6. 筛选方法的具体实现
|
||
private bool FilterSymbols(object item)
|
||
{
|
||
// 如果搜索框为空,则不过滤,显示所有项
|
||
if (string.IsNullOrEmpty(SearchText))
|
||
{
|
||
return true;
|
||
}
|
||
|
||
// 如果项是 MaterialSymbol 类型
|
||
if (item is MaterialSymbol symbol)
|
||
{
|
||
// 检查枚举的名称(不区分大小写)是否包含搜索文本
|
||
return symbol.ToString().IndexOf(SearchText, StringComparison.OrdinalIgnoreCase) >= 0;
|
||
}
|
||
|
||
return false;
|
||
}
|
||
|
||
// *** 选择修复: 直接从事件参数 e 中获取选中的项 ***
|
||
private void IconItemsControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
|
||
{
|
||
// 检查 e.AddedItems 是否有内容。AddedItems 包含了新选中的项。
|
||
if (e.AddedItems.Count > 0 && e.AddedItems[0] is MaterialSymbol selectedSymbol)
|
||
{
|
||
string xamlCode = $"<n:IconElement Symbol=\"{selectedSymbol}\" Foreground=\"{ColorPicker.SelectedColor}\"/>";
|
||
SelectedSymbolName = xamlCode;
|
||
|
||
}
|
||
// 如果列表被清空或选择被取消,e.AddedItems 为空
|
||
else if (IconItemsControl.SelectedItem == null)
|
||
{
|
||
SelectedSymbolName = string.Empty;
|
||
}
|
||
}
|
||
|
||
private void CopyCode_Click(object sender, RoutedEventArgs e)
|
||
{
|
||
if (!string.IsNullOrEmpty(SelectedSymbolName))
|
||
{
|
||
//string xamlCode = $"<local:SymbolIcon Symbol=\"{SelectedSymbolName}\" />";
|
||
|
||
Clipboard.SetText(SelectedSymbolName);
|
||
//MessageBox.Show($"代码已复制到剪贴板!\n\n{SelectedSymbolName}", "成功", MessageBoxButton.OK, MessageBoxImage.Information);
|
||
}
|
||
else
|
||
{
|
||
MessageBox.Show("请先选择一个图标。", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
|
||
}
|
||
}
|
||
|
||
public event PropertyChangedEventHandler PropertyChanged;
|
||
protected void OnPropertyChanged(string propertyName)
|
||
{
|
||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
||
}
|
||
}
|