我有一个带有标准验证规则的视图。它按预期工作,如果我在字段中键入一些文本,然后删除它,就会显示错误图标和文本。现在,我想要一个逻辑,如果用户单击保存按钮,则验证规则在字段为空时触发。我已经在网上找到了带有XAML代码隐藏的解决方案,但我希望它具有正确的MVVM模式。这可能吗?如果是,如何做到这一点?
这是视图模型:
public class CapacityTypeViewModel
{
private readonly AppDataContext _appDataContext;
private readonly CapacityTypeService _capacityTypeService;
// commands
public UICommand SaveCommand { get; set; }
// model
private CapacityTypeModel _capacityType;
public string Type { get; set; }
public CapacityTypeViewModel(AppDataContext appDataContext)
{
_appDataContext = appDataContext;
_capacityTypeService = new CapacityTypeService(_appDataContext);
SaveCommand = new UICommand(SaveCapacity, CanSave);
_capacityType = new CapacityTypeModel();
Type = _capacityType.Type;
}
private void SaveCapacity()
{
_capacityTypeService.Save(Type);
ThemedMessageBox.Show("Kapacitástípus", "A kapacitástípus sikeresen mentésre került", MessageBoxButton.OK, MessageBoxImage.Information);
}
private bool CanSave() => true;
}
这是观点的相关部分:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<dxb:ToolBarControl UseWholeRow="True" AllowQuickCustomization="False">
<dxb:BarButtonItem Content="Mentés" Glyph="{dx:DXImage 'SvgImages/Save/Save.svg'}" Command="{Binding SaveCommand}"
IsEnabled="{Binding ElementName=container, Path=(dxe:ValidationService.HasValidationError), Converter={dxmvvm:BooleanNegationConverter}}"></dxb:BarButtonItem>
</dxb:ToolBarControl>
<dxlc:LayoutControl Name="container" Grid.Row="1" dxe:ValidationService.IsValidationContainer="True">
<dxlc:LayoutGroup>
<dxlc:LayoutItem Label="Kapacitástípus" LabelPosition="Top">
<dxe:TextEdit Name="tbCapacityType">
<Binding Path="Type" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<Helpers:RequiredValidationRule FieldName="Kapacitástípus"/>
</Binding.ValidationRules>
</Binding>
</dxe:TextEdit>
</dxlc:LayoutItem>
</dxlc:LayoutGroup>
</dxlc:LayoutControl>
</Grid>
只需在执行命令之前调用验证规则。
您应该始终让绑定源(即您的视图模型(实现INotifyPropertyChanged
,以避免糟糕的性能和内存泄漏。
还建议通过实现INotifyDataErrorInfo
而不是使用绑定验证来验证视图模型。
以下示例对异常进行验证,以使示例保持简单。如前所述,建议实现INotifyDataErrorInfo
,而不是抛出异常。异常通常会对性能产生负面影响。
public class CapacityTypeViewModel
{
public ICommand SaveCommand => new UICommand(SaveCapacity, CanSave);
public string Type { get; set; }
private Dictionary<string, ValidationRule> ValidationRules { get; }
public CapacityTypeViewModel(AppDataContext appDataContext)
{
_appDataContext = appDataContext;
this.ValidationRules = new Dictionary<string, ValidationRule>
{
{ nameof(this.Type), new RequiredValidationRule {FieldName = "Kapacitástípus"} }
};
}
private void SaveCapacity()
{
if (!this.ValidationRules.TryGetValue(nameof(this.Type), out Validationrule rule)
|| !rule.Validate(this.Type, CultureInfo.CurrentCulture).IsValid)
{
// Notify the view (via the data binding) about the validation error.
// Requires the Binding to enable Binding.ValidatesOnException.
// Consider to let the view model implement INotifyDataErrorInfo instead of throwing exceptions.
throw new ArgumentException(rule.ErrorContent as string);
}
_capacityTypeService.Save(Type);
ThemedMessageBox.Show("Kapacitástípus", "A kapacitástípus sikeresen mentésre került", MessageBoxButton.OK, MessageBoxImage.Information);
}
private bool CanSave() => true;
}
查看
<dxe:TextEdit Name="tbCapacityType">
<Binding Path="Type"
ValidatesOnExceptions="True"
UpdateSourceTrigger="PropertyChanged" />
</dxe:TextEdit>