using System; using System.Linq; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using BuechermarktClient.Models; using System.Threading; using MongoDB.Driver; using System.Collections.Generic; namespace BuechermarktClient { /// /// Interaktionslogik für Students.xaml /// public partial class Students : Window { public Thread RefreshThread = null; private bool ThreadRunning = true; public bool Chooser = false; private string _SearchField; public string SearchField { get { return _SearchField; } set { if (value != _SearchField) { _SearchField = value; LoadList(); } } } public Student SelectedItem { get; private set; } public Students() : this(false){ } public Students(bool select) { DataContext = this; Chooser = select; InitializeComponent(); InitializeComponent(); RefreshThread = new Thread(RefreshThreadS); RefreshThread.Start(); RefreshThread.IsBackground = true; Closing += Students_Closing; } private void Students_Closing(object sender, System.ComponentModel.CancelEventArgs e) { ThreadRunning = false; } public void RefreshThreadS() { while (ThreadRunning) { LoadList(); Thread.Sleep(1000); } } public void LoadList() { var list = MainWindow.StudentCollection.FindSync((b) => true).ToList(); var show = new List(); if (SearchField != null && SearchField != String.Empty) { foreach (var e in list) { if (e.Forname.ToLower().Contains(SearchField.ToLower()) || e.Lastname.ToLower().Contains(SearchField.ToLower()) || e.PhoneNumber.ToLower().Contains(SearchField.ToLower()) || e.EMail.ToLower().Contains(SearchField.ToLower()) || e.Form.ToLower().Contains(SearchField.ToLower())) { show.Add(e); } } } else { show = list; } Dispatcher.BeginInvoke(new Action(delegate () { ViewList.ItemsSource = show; })); } private void AddNew_Click(object sender, RoutedEventArgs e) { var editWindow = new StudentsEdit(null) { Owner = this }; editWindow.Show(); } private void ListViewItem_PreviewMouseUp(object sender, MouseButtonEventArgs e) { if (sender is ListViewItem item && item.IsSelected) { SelectedItem = item.DataContext as Student; if (Chooser) { DialogResult = true; Close(); } else { var editWindow = new StudentsEdit(item.DataContext as Student) { Owner = this }; editWindow.Show(); } } } } }