Im using WPF/XAML (first time coding using this technology) and Im trying to send an error message when the user did not selected any of the options inside a combobox.
This is the code so far:
In my backend im using an entity like so:
public class Scanner : ValidationBase
{
private int _id;
private string _name;
//[Range(1, int.MaxValue,ErrorMessageResourceType = typeof(Scanner), ErrorMessageResourceName = "ScannerConfigurationInfo_Required")]
public int Id
{
get { return _id; }
set { SetProperty(ref _id, value); }
}
[Required(ErrorMessageResourceName = "ScannerConfigurationInfo_Required")]
public string Name
{
get { return _name; }
set { SetProperty(ref _name, value); }
}
}
And this is my ViewModel (for simplicity I took part of the code that does not impact on this question):
class ScannerSettingsViewModel : ModalViewModelBase, IScannerSettingsViewModel
{
private const string SCANNER_ID = "ScannerId";
private ObservableCollection<Scanner> _availableScanners;
private Scanner _scannerSelected;
public AwaitableDelegateCommand SaveCommand { get; private set; }
public AwaitableDelegateCommand CancelCommand { get; private set; }
[Required(ErrorMessageResourceName = "ScannerConfigurationInfo_Required")]
public Scanner ScannerSelected
{
get { return _scannerSelected; }
set { SetProperty(ref _scannerSelected, value); }
}
public ObservableCollection<Scanner> AvailableScanners
{
get { return _availableScanners; }
set { SetProperty(ref _availableScanners, value); }
}
//Services
private readonly INavigationService _navigation;
private readonly ICheckDirectService _checkDirectService;
private readonly ISession _session;
public ScannerSettingsViewModel(INavigationService navigation, ISession session,
ICheckDirectService checkDirectService)
{
_navigation = navigation;
_session = session;
_checkDirectService = checkDirectService;
SaveCommand = new AwaitableDelegateCommand(SaveCommand_Execute);
CancelCommand = new AwaitableDelegateCommand(CancelCommand_Execute);
}
private async Task SaveCommand_Execute()
{
await ScannerSelected.FireValidation();
if (ScannerSelected.HasErrors)
{
return;
}
_session.Computer.Settings[SCANNER_ID] = ScannerSelected.Id.ToString();
await _session.Computer.Settings.SaveChanges();
await CloseDialog();
}
}
And this is my XAML (again, for simplicity sake I took only the part that corresponds with the question):
<ComboBox Grid.Row ="1" ItemsSource="{Binding AvailableScanners}"
SelectedItem="{Binding ScannerConfigurationInfo.ScannerSetting, Mode=TwoWay, ValidatesOnDataErrors=True}"
SelectedValue="{Binding ScannerConfigurationInfo.ScannerSetting.Name, Mode=TwoWay, ValidatesOnDataErrors=True}"
Margin="20 50 20 50"
md:HintAssist.Hint="{l:LocText ScannerConfiguration_Model}"
DisplayMemberPath="Name"
SelectedValuePath="Name"
Style="{DynamicResource ComboboxDynamic}">
</ComboBox>
So, the combobox will not have any option selected as default (this is intended) and the idea is that, there is a save button that fires the SaveCommand_Execute
method, it DOES bring the error in the .HasErrors
, but doesn't "paint it" on the front end.