功能完善
This commit is contained in:
383
NeuWPF/NeoUITest/MainWindow.xaml.cs
Normal file
383
NeuWPF/NeoUITest/MainWindow.xaml.cs
Normal file
@@ -0,0 +1,383 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
|
||||
using NeoUI.Appearance;
|
||||
using NeoUI.Controls;
|
||||
|
||||
namespace NeumUITest;
|
||||
|
||||
/// <summary>
|
||||
/// MainWindow.xaml 的交互逻辑
|
||||
/// </summary>
|
||||
[ObservableObject]
|
||||
public partial class MainWindow
|
||||
{
|
||||
public ObservableCollection<NodeViewModel> Nodes { get; set; }
|
||||
|
||||
// 用于绑定到MultiTreeView的SelectedItems
|
||||
// 使用ObservableCollection可以自动通知UI集合变化
|
||||
public ObservableCollection<object> SelectedNodes { get; set; }
|
||||
public MainWindow()
|
||||
{
|
||||
// 创建示例数据
|
||||
Nodes = new ObservableCollection<NodeViewModel>
|
||||
{
|
||||
new NodeViewModel
|
||||
{
|
||||
Name = "亚洲",
|
||||
Children =
|
||||
{
|
||||
new NodeViewModel { Name = "中国" },
|
||||
new NodeViewModel { Name = "日本" },
|
||||
new NodeViewModel { Name = "韩国" }
|
||||
}
|
||||
},
|
||||
new NodeViewModel
|
||||
{
|
||||
Name = "欧洲",
|
||||
Children =
|
||||
{
|
||||
new NodeViewModel { Name = "德国" },
|
||||
new NodeViewModel { Name = "法国" },
|
||||
}
|
||||
},
|
||||
new NodeViewModel { Name = "北美洲" }
|
||||
};
|
||||
|
||||
// 初始化选中的集合,并预选一个节点
|
||||
SelectedNodes = new ObservableCollection<object>();
|
||||
var chinaNode = Nodes[0].Children[0];
|
||||
SelectedNodes.Add(chinaNode); // 默认选中"中国"
|
||||
|
||||
DataContext = this;
|
||||
InitNode1Value();
|
||||
|
||||
InitializeComponent();
|
||||
Items =
|
||||
[
|
||||
new RadioItem { Label = "Apple", Value = "Apple" },
|
||||
new RadioItem { Label = "Pear", Value = "Pear" },
|
||||
new RadioItem { Label = "Orange", Value = "Orange" }];
|
||||
}
|
||||
public ObservableCollection<Staff> StaffList { get; set; } = [];
|
||||
private ObservableCollection<string> _allItems;
|
||||
private IEnumerable<string> _pagedItems;
|
||||
|
||||
[ObservableProperty]
|
||||
private Staff staff;
|
||||
public void InitNode1Value()
|
||||
{
|
||||
Staff staff = new Staff() { Name = "Alice", Age = 30, Sex = "Male", Duty = "Manager", IsExpanded = true };
|
||||
Staff staff2 = new Staff() { Name = "Alice1", Age = 21, Sex = "Male", Duty = "Normal", IsExpanded = true };
|
||||
Staff staff3 = new Staff() { Name = "Alice11", Age = 21, Sex = "Male", Duty = "Normal" };
|
||||
staff2.StaffList.Add(staff3);
|
||||
staff3 = new Staff() { Name = "Alice22", Age = 21, Sex = "Female", Duty = "Normal" };
|
||||
staff2.StaffList.Add(staff3);
|
||||
staff.StaffList.Add(staff2);
|
||||
staff2 = new Staff() { Name = "Alice2", Age = 22, Sex = "Female", Duty = "Normal" };
|
||||
staff.StaffList.Add(staff2);
|
||||
staff2 = new Staff() { Name = "Alice3", Age = 23, Sex = "Female", Duty = "Normal" };
|
||||
staff.StaffList.Add(staff2);
|
||||
StaffList.Add(staff);
|
||||
|
||||
staff = new Staff() { Name = "Bob", Age = 31, Sex = "Male", Duty = "CEO" };
|
||||
staff2 = new Staff() { Name = "Bob1", Age = 24, Sex = "Female", Duty = "Normal" };
|
||||
staff.StaffList.Add(staff2);
|
||||
staff2 = new Staff() { Name = "Bob2", Age = 25, Sex = "Female", Duty = "Normal" };
|
||||
staff.StaffList.Add(staff2);
|
||||
staff2 = new Staff() { Name = "Bob3", Age = 26, Sex = "Male", Duty = "Normal" };
|
||||
staff.StaffList.Add(staff2);
|
||||
StaffList.Add(staff);
|
||||
|
||||
staff = new Staff() { Name = "Cyber", Age = 32, Sex = "Female", Duty = "Leader" };
|
||||
staff2 = new Staff() { Name = "Cyber1", Age = 27, Sex = "Female", Duty = "Normal" };
|
||||
staff.StaffList.Add(staff2);
|
||||
staff2 = new Staff() { Name = "Cyber2", Age = 28, Sex = "Female", Duty = "Normal" };
|
||||
staff.StaffList.Add(staff2);
|
||||
StaffList.Add(staff);
|
||||
}
|
||||
[ObservableProperty]
|
||||
private Area area;
|
||||
|
||||
public ObservableCollection<Area> SelectedObservableAreas { get; set; } = [];
|
||||
[ObservableProperty]
|
||||
public partial List<Area> SelectedListAreas { get; set; } = [];
|
||||
[ObservableProperty]
|
||||
private string password;
|
||||
|
||||
[ObservableProperty]
|
||||
private string input;
|
||||
[RelayCommand]
|
||||
private void AddArea()
|
||||
{
|
||||
|
||||
}
|
||||
public Area[] Areas
|
||||
{
|
||||
get;
|
||||
} =
|
||||
{
|
||||
new Area(0, "北海道"),
|
||||
new Area(1, "青森県"),
|
||||
new Area(2, "岩手県"),
|
||||
new Area(3, "宮城県"),
|
||||
new Area(4, "秋田県"),
|
||||
new Area(5, "山形県"),
|
||||
new Area(6, "福島県"),
|
||||
new Area(7, "茨城県"),
|
||||
new Area(8, "栃木県"),
|
||||
new Area(9, "群馬県"),
|
||||
new Area(10, "埼玉県"),
|
||||
new Area(11, "千葉県"),
|
||||
new Area(12, "東京都"),
|
||||
new Area(13, "神奈川県"),
|
||||
new Area(14, "新潟県"),
|
||||
new Area(15, "富山県"),
|
||||
new Area(16, "石川県"),
|
||||
new Area(17, "福井県"),
|
||||
new Area(18, "山梨県"),
|
||||
new Area(19, "長野県"),
|
||||
new Area(20, "岐阜県"),
|
||||
new Area(21, "静岡県"),
|
||||
new Area(22, "愛知県"),
|
||||
new Area(23, "三重県"),
|
||||
new Area(24, "滋賀県"),
|
||||
new Area(25, "京都府"),
|
||||
new Area(26, "大阪府"),
|
||||
new Area(27, "兵庫県"),
|
||||
new Area(28, "奈良県"),
|
||||
new Area(29, "和歌山県"),
|
||||
new Area(30, "鳥取県"),
|
||||
new Area(31, "島根県"),
|
||||
new Area(32, "岡山県"),
|
||||
new Area(33, "広島県"),
|
||||
new Area(34, "山口県"),
|
||||
new Area(35, "徳島県"),
|
||||
new Area(36, "香川県"),
|
||||
new Area(37, "愛媛県"),
|
||||
new Area(38, "高知県"),
|
||||
new Area(39, "福岡県"),
|
||||
new Area(40, "佐賀県"),
|
||||
new Area(41, "長崎県"),
|
||||
new Area(42, "熊本県"),
|
||||
new Area(43, "大分県"),
|
||||
new Area(44, "宮崎県"),
|
||||
new Area(45, "鹿児島県"),
|
||||
new Area(46, "沖縄県")
|
||||
};
|
||||
|
||||
public IEnumerable<RadioItem> Items { get; set; }
|
||||
|
||||
public struct RadioItem
|
||||
{
|
||||
public string Label;
|
||||
|
||||
public string Value;
|
||||
}
|
||||
|
||||
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<string> e)
|
||||
{ MessageBox.Show(e.NewValue); }
|
||||
|
||||
private void LeftTopButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
NotificationManager.Show(
|
||||
"发生错误",
|
||||
"无法连接到服务器,请检查您的网络连接。",
|
||||
NotificationType.Error,
|
||||
NotificationPlacement.TopLeft,
|
||||
durationSeconds: 5); // 自定义持续时间
|
||||
}
|
||||
|
||||
private void RightTopButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
NotificationManager.Show("操作成功", "您的设置已保存,并已成功应用到系统中。", NotificationType.Success);
|
||||
}
|
||||
|
||||
private void LeftBottomButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
NotificationManager.Show(
|
||||
"警告",
|
||||
"您的磁盘空间即将用尽,请及时清理文件。",
|
||||
NotificationType.Warning,
|
||||
NotificationPlacement.BottomLeft);
|
||||
|
||||
}
|
||||
|
||||
private void RightBottomButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
NotificationManager.Show(
|
||||
"系统提示",
|
||||
"这是一条普通的信息提示,用于通知用户。",
|
||||
NotificationType.Info,
|
||||
NotificationPlacement.BottomRight);
|
||||
}
|
||||
|
||||
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("加载失败,请检查您的网络连接。");
|
||||
}
|
||||
|
||||
private void ShowBasicModal_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
bool? result = Modal.Confirm(this, "Basic Dialog", "This is a basic modal dialog.");
|
||||
if (result == true)
|
||||
{
|
||||
// 处理OK逻辑
|
||||
MessageBox.Show("User clicked OK!");
|
||||
}
|
||||
}
|
||||
|
||||
// 异步关闭用法
|
||||
private async void ShowAsyncModal_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
bool? result = Modal.Confirm(this,
|
||||
"Async Dialog",
|
||||
"This dialog will close after a 2-second task.",
|
||||
async () =>
|
||||
{
|
||||
// 模拟一个耗时操作
|
||||
await Task.Delay(2000);
|
||||
// 返回true表示操作成功,可以关闭对话框
|
||||
// 返回false则对话框会保持打开状态
|
||||
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.");
|
||||
}
|
||||
|
||||
private void Icon_OnClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
IconsWindow iconsWindow = new IconsWindow
|
||||
{
|
||||
Owner = this,
|
||||
WindowStartupLocation = WindowStartupLocation.CenterOwner
|
||||
};
|
||||
iconsWindow.ShowDialog();
|
||||
|
||||
}
|
||||
|
||||
private void DarkTheme(object sender, RoutedEventArgs e)
|
||||
{
|
||||
ThemeManager.SetThemeMode(ThemeMode.Dark);
|
||||
}
|
||||
|
||||
private void LightTheme(object sender, RoutedEventArgs e)
|
||||
{
|
||||
ThemeManager.SetThemeMode(ThemeMode.Light);
|
||||
}
|
||||
|
||||
private void ColorPalette_OnClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
var palette = new ColorPaletteWindow
|
||||
{
|
||||
Owner = this,
|
||||
WindowStartupLocation = WindowStartupLocation.CenterOwner
|
||||
};
|
||||
palette.ShowDialog();
|
||||
}
|
||||
}
|
||||
public class ViewModelBase : INotifyPropertyChanged
|
||||
{
|
||||
public event PropertyChangedEventHandler PropertyChanged;
|
||||
protected void OnPropertyChanged([System.Runtime.CompilerServices.CallerMemberName] string propertyName = null)
|
||||
{
|
||||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
||||
}
|
||||
}
|
||||
|
||||
// 树节点的数据模型
|
||||
public class NodeViewModel : ViewModelBase
|
||||
{
|
||||
private string _name;
|
||||
public string Name
|
||||
{
|
||||
get => _name;
|
||||
set { _name = value; OnPropertyChanged(); }
|
||||
}
|
||||
|
||||
// 注意:这里不需要IsChecked属性,因为它由MultiTreeViewItem管理
|
||||
// 如果需要更复杂的逻辑(例如持久化选中状态),可以在这里添加
|
||||
|
||||
public ObservableCollection<NodeViewModel> Children { get; set; } = new ObservableCollection<NodeViewModel>();
|
||||
}
|
||||
public record Area(int Id, string Name)
|
||||
{
|
||||
public int Id { get; } = Id;
|
||||
|
||||
public string Name { get; } = Name;
|
||||
|
||||
public override string ToString() => Name;
|
||||
}
|
||||
|
||||
public partial class Staff : ObservableObject
|
||||
{
|
||||
[ObservableProperty]
|
||||
private string name = null!;
|
||||
|
||||
[ObservableProperty]
|
||||
private int age;
|
||||
|
||||
[ObservableProperty]
|
||||
private string sex = null!;
|
||||
|
||||
[ObservableProperty]
|
||||
private string duty = null!;
|
||||
|
||||
[ObservableProperty]
|
||||
private bool isChecked = true;
|
||||
|
||||
[ObservableProperty]
|
||||
private bool isSelected = false;
|
||||
|
||||
[ObservableProperty]
|
||||
private bool isExpanded = false;
|
||||
|
||||
[ObservableProperty]
|
||||
private ObservableCollection<Staff> staffList = [];
|
||||
}
|
||||
Reference in New Issue
Block a user