197 lines
7.3 KiB
C#
197 lines
7.3 KiB
C#
|
|
using System.Collections.ObjectModel;
|
|||
|
|
using System.Linq;
|
|||
|
|
using System.Text;
|
|||
|
|
using System.Windows;
|
|||
|
|
using System.Windows.Data;
|
|||
|
|
|
|||
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|||
|
|
using CommunityToolkit.Mvvm.Input;
|
|||
|
|
using CommunityToolkit.Mvvm.Messaging;
|
|||
|
|
|
|||
|
|
using QuickModeling.Models;
|
|||
|
|
|
|||
|
|
namespace QuickModeling.ViewModels
|
|||
|
|
{
|
|||
|
|
public partial class ConfigurationViewModel : ObservableValidator
|
|||
|
|
{
|
|||
|
|
[ObservableProperty]
|
|||
|
|
private ComponentConfig config;
|
|||
|
|
[ObservableProperty]
|
|||
|
|
private Parameter parameter = new Parameter();
|
|||
|
|
[ObservableProperty]
|
|||
|
|
[NotifyCanExecuteChangedFor(nameof(AddGroupCommand))]
|
|||
|
|
private string groupName;
|
|||
|
|
|
|||
|
|
public ObservableCollection<string> Groups { get; set; } = new ObservableCollection<string>();
|
|||
|
|
|
|||
|
|
public ConfigurationViewModel(ComponentConfig config)
|
|||
|
|
{
|
|||
|
|
this.config = config;
|
|||
|
|
var gs = config.Parameters.Select(p => p.Group).ToList();
|
|||
|
|
foreach (var group in gs)
|
|||
|
|
{
|
|||
|
|
if (!string.IsNullOrEmpty(group) && !Groups.Contains(group))
|
|||
|
|
{
|
|||
|
|
Groups.Add(group);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
var cv = CollectionViewSource.GetDefaultView(config.Parameters);
|
|||
|
|
//不包含这个分组属性,则添加,避免重复分组
|
|||
|
|
if (!cv.GroupDescriptions
|
|||
|
|
.Any(g => g is PropertyGroupDescription description && description.PropertyName == "Group"))
|
|||
|
|
{
|
|||
|
|
cv.GroupDescriptions.Add(new PropertyGroupDescription("Group"));
|
|||
|
|
}
|
|||
|
|
//Parameters = config.Parameters;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private bool CanAddGroup => !string.IsNullOrEmpty(GroupName) && !Groups.Contains(GroupName);
|
|||
|
|
|
|||
|
|
[RelayCommand(CanExecute = nameof(CanAddGroup))]
|
|||
|
|
private void AddGroup()
|
|||
|
|
{
|
|||
|
|
Groups.Add(GroupName);
|
|||
|
|
Parameter.Group = GroupName;
|
|||
|
|
GroupName = string.Empty;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
[ObservableProperty]
|
|||
|
|
private string description;
|
|||
|
|
[RelayCommand]
|
|||
|
|
private void SendConfig(Window window)
|
|||
|
|
{
|
|||
|
|
var groups = Config.Parameters.GroupBy(p => p.FullName).Where(g => g.Count() > 1).ToList();
|
|||
|
|
StringBuilder sb = new StringBuilder();
|
|||
|
|
if (groups.Any())
|
|||
|
|
{
|
|||
|
|
foreach (var group in groups)
|
|||
|
|
{
|
|||
|
|
sb.AppendLine($"参数 '{group.FirstOrDefault().FullName}' 已存在重复项");
|
|||
|
|
}
|
|||
|
|
MessageBox.Show(sb.ToString(), "重复参数", MessageBoxButton.OK, MessageBoxImage.Warning);
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
var parameterHasErrors = Config.Parameters.Where(p => p.HasErrors);
|
|||
|
|
if (parameterHasErrors.Any())
|
|||
|
|
{
|
|||
|
|
string errorMessage = string.Join(
|
|||
|
|
"\n",
|
|||
|
|
parameterHasErrors.Select(
|
|||
|
|
p => $"{p.Group}-{p.Name}: {p.GetErrors().FirstOrDefault()?.ErrorMessage}"));
|
|||
|
|
|
|||
|
|
MessageBox.Show(errorMessage);
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
ValidateAllProperties();
|
|||
|
|
if (!HasErrors)
|
|||
|
|
{
|
|||
|
|
WeakReferenceMessenger.Default.Send(Config, "config");
|
|||
|
|
window?.Close();
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
MessageBox.Show(GetErrors().FirstOrDefault().ErrorMessage);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
[RelayCommand]
|
|||
|
|
private void EditParameter(Parameter parameter)
|
|||
|
|
{
|
|||
|
|
if (parameter != null && config.Parameters.Contains(parameter))
|
|||
|
|
{
|
|||
|
|
var copy = parameter.Clone() as Parameter;
|
|||
|
|
Parameter = copy;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
[RelayCommand]
|
|||
|
|
private void DeleteParameter(Parameter parameter)
|
|||
|
|
{
|
|||
|
|
if (parameter != null && config.Parameters.Contains(parameter))
|
|||
|
|
{
|
|||
|
|
var result = MessageBox.Show(
|
|||
|
|
$"确定要删除参数 '{parameter.Name}' 吗?",
|
|||
|
|
"删除参数",
|
|||
|
|
MessageBoxButton.YesNo,
|
|||
|
|
MessageBoxImage.Question);
|
|||
|
|
if (result == MessageBoxResult.No)
|
|||
|
|
{
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
config.Parameters.Remove(parameter);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
[RelayCommand]
|
|||
|
|
private void CloseWindow(Window window) { window?.Close(); }
|
|||
|
|
[RelayCommand]
|
|||
|
|
private void AddParameter()
|
|||
|
|
{
|
|||
|
|
if (string.IsNullOrEmpty(Parameter.Name))
|
|||
|
|
{
|
|||
|
|
MessageBox.Show("参数名称不能为空,请选择后再添加。", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
if (string.IsNullOrEmpty(Parameter.Group))
|
|||
|
|
{
|
|||
|
|
MessageBox.Show("参数分组不能为空,请填写后再添加。", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
if (config.Parameters.Any(p => p.Group == Parameter.Group && p.Name == Parameter.Name))
|
|||
|
|
{
|
|||
|
|
MessageBox.Show("该分组下已存在同名参数,请修改名称后再添加。", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
var copy = Parameter.Clone() as Parameter; // 克隆参数,避免引用问题
|
|||
|
|
bool isValid = true;
|
|||
|
|
object value;
|
|||
|
|
switch (copy.DataType)
|
|||
|
|
{
|
|||
|
|
case TypeKind.Boolean:
|
|||
|
|
isValid = bool.TryParse(copy.Value?.ToString(), out bool boolValue);
|
|||
|
|
value = boolValue;
|
|||
|
|
break;
|
|||
|
|
//case TypeKind.DateTime:
|
|||
|
|
// isValid = DateTime.TryParse(copy.Value.ToString(), out DateTime dateTimeValue);
|
|||
|
|
// value = dateTimeValue;
|
|||
|
|
// break;
|
|||
|
|
case TypeKind.Double:
|
|||
|
|
isValid = double.TryParse(copy.Value?.ToString(), out double doubleValue);
|
|||
|
|
value = doubleValue;
|
|||
|
|
break;
|
|||
|
|
case TypeKind.Integer:
|
|||
|
|
isValid = int.TryParse(copy.Value?.ToString(), out int intValue);
|
|||
|
|
break;
|
|||
|
|
//case TypeKind.Point:
|
|||
|
|
// break;
|
|||
|
|
case TypeKind.String:
|
|||
|
|
value = copy.Value?.ToString() ?? string.Empty;
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
if (!isValid)
|
|||
|
|
{
|
|||
|
|
MessageBox.Show(
|
|||
|
|
$"参数 '{copy.Name}' 的值 '{copy.Value}' 无效,请检查数据类型。",
|
|||
|
|
"提示",
|
|||
|
|
MessageBoxButton.OK,
|
|||
|
|
MessageBoxImage.Warning);
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
config.Parameters.Add(copy);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 配置名称
|
|||
|
|
/// </summary>
|
|||
|
|
//[Required(ErrorMessage = "配置名称不能为空")]
|
|||
|
|
//public string ConfigName { get => configName; set => SetProperty(ref configName, value, true); }
|
|||
|
|
|
|||
|
|
///// <summary>
|
|||
|
|
///// 参数
|
|||
|
|
///// </summary>
|
|||
|
|
//public ObservableCollection<Parameter> Parameters { get; set; } = new ObservableCollection<Parameter>();
|
|||
|
|
}
|
|||
|
|
}
|