WPF AutoCompleteBox使用数据库

时间:2016-04-23 17:47:26

标签: c# wpf xaml mvvm mvvm-light

我们正在实施一个桌面应用程序,该应用程序需要动态自动完成框,其中包括名称和图像。我认为使用WPF Toolkit AutoCompleteBox看起来非常合适,但是我无法将所有内容与MVVM连接并连接到数据库。

我目前的问题与无法将Populating事件传播到ViewModel有关。它想在后面的代码中创建一个方法,我需要在我的视图模型中。似乎AutoCompleteBox没有可用的命令。

这是我的XAML

<controls1:AutoCompleteBox Name="PatientSearchBox"
                                               ItemsSource="{Binding Patients}"
                                               SelectedItem="{Binding SelectedPatient, Mode=TwoWay}"
                                               ItemTemplate="{StaticResource PatientAutoCompleteTemplate}"
                                               ItemFilter="{Binding PersonFilter}"
                                               ValueMemberPath="DisplayName"
                                               Command="{Binding PopulatingCommand}">
                    </controls1:AutoCompleteBox>

ViewModel

public class PatientSearchViewModel : INotifyPropertyChanged
    {
        private IPatientService _patientService;

        private IEnumerable<PatientSearch> _patients;
        private PatientSearch _selectedPatient;

        public RelayCommand<PopulatingEventArgs> PopulatingCommand { get; set; }

        public PatientSearchViewModel()
        {
            PopulatingCommand = new RelayCommand<PopulatingEventArgs>(OnPopulatingCommand);
            _patientService = new PatientService();
        }

        public IEnumerable<PatientSearch> Patients
        {
            get { return _patients; }
            set
            {
                _patients = value;
                OnPropertyChanged();
            }
        }

        public PatientSearch SelectedPatient
        {
            get { return _selectedPatient; }
            set
            {
                _selectedPatient = value;
            }
        }

        public AutoCompleteFilterPredicate<object> PersonFilter
        {
            get
            {
                return (searchText, obj) =>
                (obj as PatientSearch).FirstName.Contains(searchText)
                || (obj as PatientSearch).LastName.Contains(searchText);
            }
        } 

        public event PropertyChangedEventHandler PropertyChanged;

        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            var handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }

        private void OnPopulatingCommand(PopulatingEventArgs e)
        {
            Patients = _patientService.SearchPatients(e.Parameter);
        }
    }

0 个答案:

没有答案