using System; using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Input; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using Melskin.Appearance; using Melskin.Controls; namespace VibeWPFTest; /// /// MainWindow.xaml 的交互逻辑 /// [ObservableObject] public partial class MainWindow { #region 数据集合 public ObservableCollection HierarchicalData { get; set; } public ObservableCollection DataItems { get; set; } public ObservableCollection SelectedDataItems { get; set; } public ObservableCollection Nodes { get; set; } public ObservableCollection SelectedNodes { get; set; } public ObservableCollection StaffList { get; set; } = []; public ObservableCollection SelectedObservableAreas { get; set; } = []; #endregion #region 属性 [ObservableProperty] public partial TreeNodeItem SelectedItem { get; set; } [ObservableProperty] public partial Area AutoArea { get; set; } [ObservableProperty] public partial Area SearchArea { get; set; } partial void OnSearchAreaChanged(Area value) { MessageBox.Show($"搜索选择了:{value?.Name}"); } private Area? _area; public Area? Area { get => _area; set { Debug.WriteLine($"VM.Area setter: incoming = {value?.Name ?? "null"}"); //if (value == null) //{ // // 打印调用栈,帮助定位是谁把它设为 null(只在收到 null 时打印以减少噪音) // var st = new System.Diagnostics.StackTrace(true); // Debug.WriteLine("StackTrace for setting Area = null:"); // Debug.WriteLine(st.ToString()); //} _area = value; OnPropertyChanged(); } } [ObservableProperty] public partial List SelectedListAreas { get; set; } = []; [ObservableProperty] public partial string Password { get; set; } [ObservableProperty] public partial string Input { get; set; } #endregion #region 命令 public ICommand ShowSelectedItemsCommand { get; set; } public ICommand ToggleDetailsCommand { get; set; } [RelayCommand] private void AddArea() { MessageBox.Show($"多选1:{SelectedListAreas.Count}\n多选2:{SelectedObservableAreas.Count}\n单选:{Area?.Name}"); //StringBuilder sb = new StringBuilder(); //var dictionary = Application.Current.Resources; //foreach (var res in dictionary.MergedDictionaries) //{ // foreach (DictionaryEntry item in res) // { // //sb.AppendLine($"{item.Key} = {item.Value}"); // if (item.Key.ToString() == "TextPrimaryBrush") // { // MessageBox.Show($"TextPrimaryBrush:{item.Value}"); // } // if (item.Key.ToString() == "TextPrimaryBrush") // { // MessageBox.Show($"TextSecondaryBrush:{item.Value}"); // } // } //} //输出到桌面 //System.IO.File.WriteAllText(System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "resources.txt"), sb.ToString()); } #endregion #region 静态数据 public Area[] Areas { get; } = Enumerable.Range(0, 47) .Select(i => new Area(i, GetJapaneseRegionName(i))) .ToArray(); public ObservableCollection Items { get; set; } public class RadioItem { public string Label { get; set; } public string Value { get; set; } } #endregion public MainWindow() { InitializeData(); InitializeCommands(); DataContext = this; InitializeComponent(); } #region 初始化方法 private void InitializeData() { // 初始化层级数据 HierarchicalData = new ObservableCollection { new("Phase 1: Project Planning", "Alice", "Completed") { Children = { new("Define project scope", "Alice", "Completed"), new("Create project timeline", "Bob", "Completed") } }, new("Phase 2: Development", "Charlie", "In Progress") { Children = { new("Backend development", "David", "In Progress") { Children = { new("Setup database", "David", "Completed"), new("Develop APIs", "Eve", "In Progress") } }, new("Frontend development", "Frank", "Not Started") } }, new("Phase 3: Deployment", "Grace", "Not Started") }; // 初始化人员数据 DataItems = new ObservableCollection { new("John Doe", 30), new("Jane Smith", 25), new("Sam Williams", 42), new("Peter Jones", 55) }; // 初始化地区数据 Nodes = new ObservableCollection { new("亚洲") { Children = { new("中国"), new("日本"), new("韩国") } }, new("欧洲") { Children = { new("德国"), new("法国") } }, new("北美洲") }; // 初始化员工数据 InitializeStaffData(); // 初始化单选项 Items = new ObservableCollection() { new RadioItem { Label = "Apple", Value = "Apple" }, new RadioItem { Label = "Pear", Value = "Pear" }, new RadioItem { Label = "Orange", Value = "Orange" } }; // 初始化选中集合 SelectedDataItems = new ObservableCollection(); SelectedNodes = new ObservableCollection { Nodes[0].Children[0] }; // 默认选中"中国" } private void InitializeStaffData() { var alice = new TreeNodeItem("Alice", 30, "Male", "Manager") { IsExpanded = true }; alice.Children.Add(new TreeNodeItem("Alice1", 21, "Male", "Normal") { IsExpanded = true, Children = { new("Alice11", 21, "Male", "Normal"), new("Alice22", 21, "Female", "Normal") } }); alice.Children.Add(new TreeNodeItem("Alice2", 22, "Female", "Normal")); alice.Children.Add(new TreeNodeItem("Alice3", 23, "Female", "Normal")); var bob = new TreeNodeItem("Bob", 31, "Male", "CEO"); bob.Children.Add(new TreeNodeItem("Bob1", 24, "Female", "Normal")); bob.Children.Add(new TreeNodeItem("Bob2", 25, "Female", "Normal")); bob.Children.Add(new TreeNodeItem("Bob3", 26, "Male", "Normal")); var cyber = new TreeNodeItem("Cyber", 32, "Female", "Leader"); cyber.Children.Add(new TreeNodeItem("Cyber1", 27, "Female", "Normal")); cyber.Children.Add(new TreeNodeItem("Cyber2", 28, "Female", "Normal")); StaffList = new ObservableCollection { alice, bob, cyber }; } private void InitializeCommands() { ShowSelectedItemsCommand = new RelayCommand(ShowSelectedItems); ToggleDetailsCommand = new RelayCommand(ToggleDetails); } #endregion #region 命令实现 private void ShowSelectedItems() { string message = $"当前选中了 {SelectedDataItems.Count} 项:\n"; message += string.Join("\n", SelectedDataItems.Select(item => item.Name)); MessageBox.Show(message); } private static void ToggleDetails(PersonItem? product) { product?.IsDetailsVisible = !product.IsDetailsVisible; } #endregion #region 静态方法 private static string GetJapaneseRegionName(int index) { string[] names = { "北海道", "青森県", "岩手県", "宮城県", "秋田県", "山形県", "福島県", "茨城県", "栃木県", "群馬県", "埼玉県", "千葉県", "東京都", "神奈川県", "新潟県", "富山県", "石川県", "福井県", "山梨県", "長野県", "岐阜県", "静岡県", "愛知県", "三重県", "滋賀県", "京都府", "大阪府", "兵庫県", "奈良県", "和歌山県", "鳥取県", "島根県", "岡山県", "広島県", "山口県", "徳島県", "香川県", "愛媛県", "高知県", "福岡県", "佐賀県", "長崎県", "熊本県", "大分県", "宮崎県", "鹿児島県", "沖縄県" }; return index < names.Length ? names[index] : $"Region{index}"; } #endregion #region 事件处理 private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (!_loaded) return; TextMessage.Text = "选项变化" + e.OriginalSource; } private bool _loaded = false; private void Window_Loaded(object sender, RoutedEventArgs e) { _loaded = true; } private void Switch_OnClick(object sender, RoutedEventArgs e) { ThemeManager.SwitchThemeMode(); } private void Test_OnClick(object sender, RoutedEventArgs e) { new ControlTestWindow().Show(); } private void Breadcrumb_Navigate(object sender, RoutedPropertyChangedEventArgs e) { MessageBox.Show(e.NewValue); } #region 通知按钮事件 private void LeftTopButton_Click(object sender, RoutedEventArgs e) { Notification.Show("发生错误", "无法连接到服务器,请检查您的网络连接。", NotificationType.Error, NotificationPlacement.TopLeft, durationSeconds: 5); } private void RightTopButton_Click(object sender, RoutedEventArgs e) { Notification.Show("操作成功", "您的设置已保存,并已成功应用到系统中。", NotificationType.Success); } private void LeftBottomButton_Click(object sender, RoutedEventArgs e) { Notification.Show("警告", "您的磁盘空间即将用尽,请及时清理文件。", NotificationType.Warning, NotificationPlacement.BottomLeft); } private void RightBottomButton_Click(object sender, RoutedEventArgs e) { Notification.Show("系统提示", "这是一条普通的信息提示,用于通知用户。", NotificationType.Info, NotificationPlacement.BottomRight); } #endregion #region Toast 事件 private void Info_Click(object sender, RoutedEventArgs e) { Toast.Screen.ShowInfo("这是一条桌面通知,显示在屏幕。"); } private void Success_Click(object sender, RoutedEventArgs e) { Toast.For(this).ShowSuccess("操作成功!已在当前窗口内显示。"); } private void Warning_Click(object sender, RoutedEventArgs e) { Toast.Screen.ShowWarning("这是一条桌面通知,请及时续订。"); } private void Error_Click(object sender, RoutedEventArgs e) { Toast.For(this).ShowError("加载失败,请检查您的网络连接。"); } #endregion #region Modal 事件 private void ShowBasicModal_Click(object sender, RoutedEventArgs e) { bool? result = Modal.Confirm(this, "默认对话框", "默认对话框"); if (result == true) { MessageBox.Show("User clicked OK!"); } } private void ShowAsyncModal_Click(object sender, RoutedEventArgs e) { bool? result = Modal.Confirm(this, "异步对话框", "对话框将在2s后关闭", async () => { await Task.Delay(2000); return true; }); if (result == true) { MessageBox.Show("Async task completed and user clicked OK!"); } } private void ShowInfoModal_Click(object sender, RoutedEventArgs e) { Modal.Info(this, "Information", "This is an information dialog."); } #endregion #region 主题相关事件 private void Icon_OnClick(object sender, RoutedEventArgs e) { IconsWindow iconsWindow = new IconsWindow { Owner = this, WindowStartupLocation = WindowStartupLocation.CenterOwner }; iconsWindow.ShowDialog(); } private async void ThemeToggle_OnCheckedChanged(object sender, RoutedEventArgs e) { try { if (sender is not ToggleButton) return; //var targetMode = tb.IsChecked == true ? ThemeMode.Dark : ThemeMode.Light; //await ThemeManager.ApplyThemeAsync(targetMode, animationDurationMs: 800); await ThemeManager.SwitchThemeModeAnimatedAsync(800); } catch (Exception exception) { Console.WriteLine(exception); } } private void ColorPalette_OnClick(object sender, RoutedEventArgs e) { var palette = new ColorPaletteWindow { Owner = this, WindowStartupLocation = WindowStartupLocation.CenterOwner }; palette.ShowDialog(); } private void PrimaryColorSelectComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (sender is ComboBox box) { ThemePalette palette = box.SelectedIndex switch { 0 => ThemePalette.Blue, 1 => ThemePalette.Green, 2 => ThemePalette.Purple, _ => ThemePalette.Blue }; ThemeManager.ApplyThemePalette(palette); } } #endregion #endregion private void ShowNeumorphism_Click(object sender, RoutedEventArgs e) { NeumorphismWindow window = new NeumorphismWindow { Owner = this, WindowStartupLocation = WindowStartupLocation.CenterOwner }; window.ShowDialog(); } } #region 数据模型 /// /// 统一的树形节点数据模型,替代 Item、NodeViewModel、Staff、TaskItem /// public partial class TreeNodeItem : ObservableObject { [ObservableProperty] public partial string Name { get; set; } = string.Empty; [ObservableProperty] public partial int Age { get; set; } [ObservableProperty] public partial string Sex { get; set; } = string.Empty; [ObservableProperty] public partial string Duty { get; set; } = string.Empty; [ObservableProperty] public partial string Status { get; set; } = string.Empty; [ObservableProperty] public partial string Owner { get; set; } = string.Empty; [ObservableProperty] public partial bool IsDetailsVisible { get; set; } = false; [ObservableProperty] public partial bool IsChecked { get; set; } = true; [ObservableProperty] public partial bool IsSelected { get; set; } = false; [ObservableProperty] public partial bool IsExpanded { get; set; } = false; public ObservableCollection Children { get; set; } = new(); public TreeNodeItem() { } public TreeNodeItem(string name) : this() { Name = name; } public TreeNodeItem(string name, string owner, string status) : this(name) { Owner = owner; Status = status; } public TreeNodeItem(string name, int age, string sex, string duty) : this(name) { Age = age; Sex = sex; Duty = duty; } } /// /// 简化的人员数据模型 /// public partial class PersonItem : ObservableObject { [ObservableProperty] public partial string Name { get; set; } = string.Empty; [ObservableProperty] public partial int Age { get; set; } [ObservableProperty] public partial bool IsDetailsVisible { get; set; } = false; public PersonItem() { } public PersonItem(string name, int age) { Name = name; Age = age; } } public class Area { public Area() { } public Area(int Id, string Name) { this.Id = Id; this.Name = Name; } public int Id { get; set; } public string Name { get; set; } public override string ToString() => Name; } #endregion