必需的数据注释无法使用XAML正常工作

时间:2019-06-27 12:24:29

标签: c# wpf xaml

我正在使用WPF / XAML(使用此技术的首次编码),当用户未选择组合框内的任何选项时,我正在尝试发送错误消息。

这是到目前为止的代码:

在我的后端即时消息中,使用这样的实体:

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); }
    }
}

这是我的ViewModel(为简单起见,我参加了部分代码,该代码不影响此问题):

 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();
        }
    }

这是我的XAML(同样,为简单起见,我只参加了与问题相对应的部分):

    <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>

因此,组合框不会选择任何默认选项(这是有意的),其想法是,有一个保存按钮会触发SaveCommand_Execute方法,但确实会在{{ 1}},但不会在前端“绘制”它。

1 个答案:

答案 0 :(得分:0)

问题出在我在ComboBoxSelectedValue的{​​{1}}上。那些指向另一个属性而不是SelectedItem,并且指向SelectedScannerDisplayMemberPath指向SelectedValuePath而不是Name