I have a view with a standard validation rule. It works as expected, if I tpye some text in the field, and then delete it, error icon and text are showing. Now, I would like to have the logic, if the user clicks the save button, the validation rule triggers if the field is empty. I have found on the net solutions with XAML codebehind, but I would like to have it in a proper MVVM pattern. Is this even possible? If yes, how to do that?
This is the viewmodel:
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;
}
And this is the related part of the view:
<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>