using System; using Prism.Mvvm; using XP.Common.Localization; namespace XP.Common.GeneralForm.ViewModels { /// /// 输入对话框 ViewModel | Input dialog ViewModel /// public class InputDialogViewModel : BindableBase { private readonly Func? _validate; private string _inputText; private string? _validationError; /// /// 窗口标题 | Window title /// public string Title { get; } /// /// 提示文本 | Prompt text /// public string Prompt { get; } /// /// 确定按钮文本(多语言)| OK button text (localized) /// public string OkText { get; } /// /// 取消按钮文本(多语言)| Cancel button text (localized) /// public string CancelText { get; } /// /// 用户输入的文本 | User input text /// public string InputText { get => _inputText; set { if (SetProperty(ref _inputText, value)) { // 输入变化时清除验证错误 | Clear validation error on input change ValidationError = null; } } } /// /// 验证错误信息,null 表示无错误 | Validation error message, null means no error /// public string? ValidationError { get => _validationError; set { if (SetProperty(ref _validationError, value)) { RaisePropertyChanged(nameof(HasValidationError)); } } } /// /// 是否存在验证错误 | Whether there is a validation error /// public bool HasValidationError => !string.IsNullOrEmpty(ValidationError); /// /// 构造函数 | Constructor /// /// 提示文本 | Prompt text /// 窗口标题 | Window title /// 默认值 | Default value /// 验证委托,返回 null 表示通过,返回错误信息则显示 | Validation delegate public InputDialogViewModel( string prompt, string title, string defaultValue = "", Func? validate = null) { Prompt = prompt; Title = title; _inputText = defaultValue; _validate = validate; // 按钮文本使用多语言 | Button text uses localization OkText = LocalizationHelper.Get("Button_OK"); CancelText = LocalizationHelper.Get("Button_Cancel"); } /// /// 执行验证,返回是否通过 | Run validation, returns whether it passed /// /// true 表示验证通过 | true means validation passed public bool Validate() { if (_validate == null) return true; var error = _validate(InputText); ValidationError = error; return error == null; } } }