使用 caliburn micro 和 fluent 验证通过按钮进行验证
Posted
技术标签:
【中文标题】使用 caliburn micro 和 fluent 验证通过按钮进行验证【英文标题】:Validate with button using caliburn micro and fluent Validation 【发布时间】:2021-01-10 22:22:17 【问题描述】:我想使用 caliburn micro 和 FluentValidation 显示对按钮事件的验证。验证规则当前按预期显示,但在单击按钮之前我无法隐藏它们。 Wpf 相当新,所以请放轻松。我试图绑定 ValidatesOnDataErrors 但无法绑定。此外,我试图隐藏显示错误的 ControlTemplate
我的观点
<UserControl.Resources>
<BooleanToVisibilityConverter x:Key="booleanVisibilityConverter"/>
</UserControl.Resources>
<Canvas FocusManager.FocusedElement="Binding ElementName=box">
<!-- Sets the focus to start with textbox-->
<Button x:Name="ValidateText" Content="Validate" Canvas.Left="234" Canvas.Top="265" Width="301" Height="29" />
<TextBox Height="23" Canvas.Left="205" TextWrapping="Wrap" Text="Binding Name, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True"
Canvas.Top="187" Width="330" TabIndex="0" x:Name="box" >
<Validation.ErrorTemplate >
<ControlTemplate >
<StackPanel Orientation="Horizontal" >
<!-- Placeholder for the TextBox itself -->
<AdornedElementPlaceholder x:Name="textBox" />
<TextBlock Margin="10" Text="Binding [0].ErrorContent" Foreground="Red" />
</StackPanel>
</ControlTemplate>
</Validation.ErrorTemplate>
</TextBox>
</Canvas>
验证:
public class UserValidator : AbstractValidator<TestViewModel>
public UserValidator()
RuleFor(x => x.Name)
.NotEmpty()
.WithMessage("No name entered");
视图模型:
public class TestViewModel : Screen, IDataErrorInfo
public bool Hello get; set; = false;
private readonly UserValidator _userValidator;
private bool _showHideError = false;
public bool ShowHideError
get return _showHideError;
set _showHideError = value;
NotifyOfPropertyChange(() => ShowHideError);
public TestViewModel()
_userValidator = new UserValidator();
public string Name get; set;
private string _lastName;
public string LastName
get return _lastName;
set
_lastName = value;
NotifyOfPropertyChange(() => LastName);
public string this[string columnName]
get
var firstOrDefault = _userValidator.Validate(this).Errors.FirstOrDefault(lol => lol.PropertyName == columnName);
if (firstOrDefault != null)
return _userValidator != null ? firstOrDefault.ErrorMessage : "";
return "";
public string Error
get
if (_userValidator != null)
FluentValidation.Results.ValidationResult results = _userValidator.Validate(this);
if (results != null && results.Errors.Any())
string errors = string.Join(Environment.NewLine, results.Errors.Select(x => x.ErrorMessage).ToArray());
return errors;
return string.Empty;
public void ValidateText()
//Display Error on View when this is fired
【问题讨论】:
我不明白你的问题,你能展示你有什么和你想要什么吗...你说一切都好,但你想隐藏......?? 我希望仅在单击按钮时在 HomePageView 上显示错误消息“未输入名称”。现在它一直在验证。 github.com/zenloki/TestButtonValidation 【参考方案1】:想通了!我创建了一个名为 Validation 的标志,并在 HomepageViewModel 加载时将其设置为 false。然后我在以下代码中检查验证是否为真:
public string this[string columnName]
get
if (!Validation)
return null;
var firstOrDefault = _userValidator.Validate(this).Errors.FirstOrDefault(lol => lol.PropertyName == columnName);
if (firstOrDefault != null)
return _userValidator != null ? firstOrDefault.ErrorMessage : "";
return "";
public string Error
get
if (Validation)
if (_userValidator != null)
FluentValidation.Results.ValidationResult results = _userValidator.Validate(this);
if (results != null && results.Errors.Any())
string errors = string.Join(Environment.NewLine, results.Errors.Select(x => x.ErrorMessage).ToArray());
return errors;
return string.Empty;
当调用按钮方法时,我将 flag(validation) 设置为 true,并且 notify 属性更改为更新视图,如下所示:
Validation = true; // turn on validation
NotifyOfPropertyChange(null); //refresh the properties/view
工作示例位于: https://github.com/zenloki/TestButtonValidation
希望这对将来的某人有所帮助
【讨论】:
以上是关于使用 caliburn micro 和 fluent 验证通过按钮进行验证的主要内容,如果未能解决你的问题,请参考以下文章
使用 Caliburn.Micro 4.0.x 和 WPF 的对话框
Caliburn.Micro 能否很好地与用户控件配合使用?