gpt4 book ai didi

c# - 如何禁用按钮,直到在 wpf mvvm 中进行良好验证

转载 作者:行者123 更新时间:2023-12-03 10:25:37 26 4
gpt4 key购买 nike

在良好的文本框验证之前,如何禁用按钮?
我已经做了一些事情,但我不知道如何隐藏按钮,直到使用 MVVM 模式以正确的方式进行良好的验证。
我仍在学习这种 MVVM 模式。
这是我的代码片段:

验证规则类:

public class NotEmptyValidationRule : ValidationRule
{
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
return string.IsNullOrWhiteSpace((value ?? "").ToString())
? new ValidationResult(false, "Field is required")
: ValidationResult.ValidResult;
}
}

xml:
<TextBox
Style="{StaticResource MaterialDesignFilledTextFieldTextBox}"
AcceptsReturn="True"
TextWrapping="Wrap"
materialDesign:HintAssist.Hint="Content"
IsEnabled="{Binding Path=IsChecked,
ElementName=MaterialDesignOutlinedTextFieldTextBoxEnabledComboBox}"
MaxLength="1000" materialDesign:ValidationAssist.UsePopup="True">
<TextBox.Text>
<Binding Path="Task.Content" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<domain:NotEmptyValidationRule
ValidatesOnTargetUpdated="True" />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>

View 模型:
private ICommand _submitCommand;
public ICommand SubmitCommand
{
get
{
if (_submitCommand == null)
{
_submitCommand = new RelayCommand(SubmitExecute, anSubmitExecute, false);
}
return _submitCommand;
}
}

private void SubmitExecute(object parameter)
{
Task.Id++;
Tasks.Add(Task);
}

private bool CanSubmitExecute(object parameter)
{
// What can I do to prevent to show this button when validation
// failed
return true;
}

最佳答案

您应该执行 INotifyDataErrorInfo您的Task中的界面类,或在您的 View 模型类中实现它并包装数据绑定(bind)Task.Content属性,例如:

private RelayCommand _submitCommand;
public ICommand SubmitCommand
{
get
{
if (_submitCommand == null)
{
_submitCommand = new RelayCommand(SubmitExecute, CanSubmitExecute, false);
}
return _submitCommand;
}
}

private void SubmitExecute(object parameter)
{
Task.Id++;
Tasks.Add(Task);
}

public string Content
{
get { return Task.Content; }
set
{
Task.Content = value;
if (string.IsNullOrEmpty(Task.Content))
_validationErrors[nameof(Content)] = "Field is required";
else
_validationErrors.Remove(nameof(Content));
ErrorsChanged?.Invoke(this, new DataErrorsChangedEventArgs(nameof(Content)));
_submitCommand.RaiseCanExecuteChanged();
}
}

private bool CanSubmitExecute(object parameter)
{
return HasErrors;
}

#region INotifyDataErrorInfo
private readonly Dictionary<string, string> _validationErrors = new Dictionary<string, string>();

public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;

public System.Collections.IEnumerable GetErrors(string propertyName)
{
if (string.IsNullOrEmpty(propertyName)
|| !_validationErrors.ContainsKey(propertyName))
return null;

return new string[1] { _validationErrors[propertyName] };
}

public bool HasErrors
{
get { return _validationErrors.Count > 0; }
}
#endregion

XAML:
<TextBox Text="{Binding Content, UpdateSourceTrigger=PropertyChanged, ValidatesOnNotifyDataErrors=True}" />

您不应使用验证规则。它们对 MVVM 不友好。请引用 this博客文章以获取更多信息。

您还需要提高 CanExecuteChanged ICommand 的事件.如何做到这一点取决于实现。大多数实现都有一个类似 RaiseCanExecuteChanged() 的方法。或类似的。

WPF 4.5: Validating Data in Using the INotifyDataErrorInfo Interface

关于c# - 如何禁用按钮,直到在 wpf mvvm 中进行良好验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56394736/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com