From 432560c7b4a09420a5252b03a1053c8b4f9529e4 Mon Sep 17 00:00:00 2001 From: Sascha Woitschetzki Date: Thu, 15 Jul 2021 11:30:40 +0200 Subject: [PATCH] merged AllContactsView into ShellView due to notification problems --- Gremlin/MVVM/MainWindow.xaml.cs | 2 +- Gremlin/MVVM/QuoteUI.xaml.cs | 2 +- .../MVVM/ViewModels/AllContactsViewModel.cs | 55 ------------- Gremlin/MVVM/ViewModels/ContactViewModel.cs | 2 +- Gremlin/MVVM/ViewModels/ShellViewModel.cs | 77 ++++++++++++++++++- Gremlin/MVVM/Views/AllContactsView.xaml | 15 ---- Gremlin/MVVM/Views/AllContactsView.xaml.cs | 19 ----- Gremlin/MVVM/Views/QuoteView.xaml | 12 ++- Gremlin/MVVM/Views/ShellView.xaml | 11 ++- 9 files changed, 97 insertions(+), 98 deletions(-) delete mode 100644 Gremlin/MVVM/ViewModels/AllContactsViewModel.cs delete mode 100644 Gremlin/MVVM/Views/AllContactsView.xaml delete mode 100644 Gremlin/MVVM/Views/AllContactsView.xaml.cs diff --git a/Gremlin/MVVM/MainWindow.xaml.cs b/Gremlin/MVVM/MainWindow.xaml.cs index 3402bf8..3d85ab3 100644 --- a/Gremlin/MVVM/MainWindow.xaml.cs +++ b/Gremlin/MVVM/MainWindow.xaml.cs @@ -115,7 +115,7 @@ namespace Gremlin.MVVM private void BtnShowContactList_Click(object sender, RoutedEventArgs e) { - ObservableCollection contacts = new(AllContactsViewModel.GetAllContactsVM()); + ObservableCollection contacts = new(_AllContactsViewModel.GetAllContactsVM()); dg_Test.ItemsSource = contacts; } } diff --git a/Gremlin/MVVM/QuoteUI.xaml.cs b/Gremlin/MVVM/QuoteUI.xaml.cs index ff4a393..21c960e 100644 --- a/Gremlin/MVVM/QuoteUI.xaml.cs +++ b/Gremlin/MVVM/QuoteUI.xaml.cs @@ -25,7 +25,7 @@ namespace Gremlin.MVVM private void LoadContacts() { - listCollectionContacts = new(AllContactsViewModel.GetAllContactsVM()); + listCollectionContacts = new(_AllContactsViewModel.GetAllContactsVM()); listCollectionContacts.Filter = ContactViewModel.SearchContact(listCollectionContacts, tbContactSearch.Text); dgFoundContacts.ItemsSource = listCollectionContacts; UpdateUI(); diff --git a/Gremlin/MVVM/ViewModels/AllContactsViewModel.cs b/Gremlin/MVVM/ViewModels/AllContactsViewModel.cs deleted file mode 100644 index ca56c4f..0000000 --- a/Gremlin/MVVM/ViewModels/AllContactsViewModel.cs +++ /dev/null @@ -1,55 +0,0 @@ -using Caliburn.Micro; -using Gremlin.GremlinData.DBClasses; -using Gremlin.GremlinData.EntityClasses; -using Microsoft.EntityFrameworkCore; -using System; -using System.Collections.Generic; -using System.Collections.ObjectModel; -using System.Diagnostics; -using System.Linq; - -namespace Gremlin.MVVM -{ - public class AllContactsViewModel : PropertyChangedBase - { - private ContactViewModel _selectedContact; - private ObservableCollection _listOfContactsVM; - - public ContactViewModel SelectedContact { get => _selectedContact; set { _selectedContact = value; NotifyOfPropertyChange(() => SelectedContact); } } - - public ObservableCollection ListOfContactsVM - { - get => _listOfContactsVM; - set { _listOfContactsVM = value; NotifyOfPropertyChange(() => ListOfContactsVM); } - } - - public AllContactsViewModel() => ListOfContactsVM = GetAllContactsVM(); - - internal static ObservableCollection GetAllContactsVM() - { - try - { - using (GremlinContext gremlinContext = new()) - { - List contacts = gremlinContext.Contacts.Include(contact => contact.Account).ToList(); - ObservableCollection contactsVM = new(); - - foreach (Contact contact in contacts) - { - contactsVM.Add(ContactViewModel.ConvertContactToVM(contact)); - } - - Debug.WriteLine($"Es wurden {contactsVM.Count} Kontakte geladen."); - return contactsVM; - } - } - catch (Exception ex) - { - ErrorHandler.ShowErrorInMessageBox(ex); - return null; - throw; - } - } - - } -} \ No newline at end of file diff --git a/Gremlin/MVVM/ViewModels/ContactViewModel.cs b/Gremlin/MVVM/ViewModels/ContactViewModel.cs index 7c41162..3b06235 100644 --- a/Gremlin/MVVM/ViewModels/ContactViewModel.cs +++ b/Gremlin/MVVM/ViewModels/ContactViewModel.cs @@ -17,7 +17,7 @@ namespace Gremlin.MVVM private string _eMail = "email@email.de"; private string _accountName = "accountName"; private string _accountStreet = "accountStreet"; - private uint _accountZIP = 0; + private uint _accountZIP; private string _accountCity = "accountCity"; public byte Gender { get => _gender; set { _gender = value; NotifyOfPropertyChange(() => Gender); } } diff --git a/Gremlin/MVVM/ViewModels/ShellViewModel.cs b/Gremlin/MVVM/ViewModels/ShellViewModel.cs index 36e1b34..00578cb 100644 --- a/Gremlin/MVVM/ViewModels/ShellViewModel.cs +++ b/Gremlin/MVVM/ViewModels/ShellViewModel.cs @@ -1,19 +1,90 @@ using Caliburn.Micro; +using Gremlin.GremlinData.DBClasses; +using Gremlin.GremlinData.EntityClasses; using Gremlin.Operations; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Diagnostics; +using System.Linq; namespace Gremlin.MVVM { public class ShellViewModel : PropertyChangedBase { - public AllContactsViewModel AllContactsVM { get; set; } - public QuoteViewModel QuoteVM { get; set; } + //public AllContactsViewModel AllContactsVM { get; set; } + + private ContactViewModel _selectedContact; + private ObservableCollection _contacts; + private QuoteViewModel _quoteVM; + + public QuoteViewModel QuoteVM + { + get => _quoteVM; + set + { + _quoteVM = value; + NotifyOfPropertyChange(() => QuoteVM); + } + } + + public ContactViewModel SelectedContact + { + get => _selectedContact; + set + { + _selectedContact = value; + NotifyOfPropertyChange(() => SelectedContact); + QuoteVM.Recipient = value; + NotifyOfPropertyChange(() => QuoteVM.Recipient); + } + } + + public ObservableCollection Contacts + { + get => _contacts; + set + { + _contacts = value; + NotifyOfPropertyChange(() => Contacts); + } + } public ShellViewModel() { - AllContactsVM = new(); + //AllContactsVM = new(); QuoteVM = new(); + Contacts = GetAllContactsVM(); + } + + internal static ObservableCollection GetAllContactsVM() + { + try + { + using (GremlinContext gremlinContext = new()) + { + List contacts = gremlinContext.Contacts.Include(contact => contact.Account).ToList(); + ObservableCollection contactsVM = new(); + + foreach (Contact contact in contacts) + { + contactsVM.Add(ContactViewModel.ConvertContactToVM(contact)); + } + + Debug.WriteLine($"Es wurden {contactsVM.Count} Kontakte geladen."); + return contactsVM; + } + } + catch (Exception ex) + { + ErrorHandler.ShowErrorInMessageBox(ex); + return null; + throw; + } } + public void CreateTex() { TexHandler.CreateTexAndOpen(QuoteVM); diff --git a/Gremlin/MVVM/Views/AllContactsView.xaml b/Gremlin/MVVM/Views/AllContactsView.xaml deleted file mode 100644 index eb3d31e..0000000 --- a/Gremlin/MVVM/Views/AllContactsView.xaml +++ /dev/null @@ -1,15 +0,0 @@ - - - - - - - - - - - - - \ No newline at end of file diff --git a/Gremlin/MVVM/Views/AllContactsView.xaml.cs b/Gremlin/MVVM/Views/AllContactsView.xaml.cs deleted file mode 100644 index 49177c7..0000000 --- a/Gremlin/MVVM/Views/AllContactsView.xaml.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System.Diagnostics; -using System.Windows.Controls; - -namespace Gremlin.MVVM -{ - public partial class AllContactsView : UserControl - { - public AllContactsView() - { - InitializeComponent(); - } - - private void ListOfContactsVM_SelectionChanged(object sender, SelectionChangedEventArgs e) - { - ContactViewModel selectedContact = (sender as DataGrid).SelectedItem as ContactViewModel; - Debug.WriteLine("Es wurde folgender Kontakt ausgewählt: " + selectedContact.LastName); - } - } -} diff --git a/Gremlin/MVVM/Views/QuoteView.xaml b/Gremlin/MVVM/Views/QuoteView.xaml index 28ac865..7ddc306 100644 --- a/Gremlin/MVVM/Views/QuoteView.xaml +++ b/Gremlin/MVVM/Views/QuoteView.xaml @@ -17,7 +17,7 @@ - + @@ -64,7 +64,15 @@ - + \ No newline at end of file diff --git a/Gremlin/MVVM/Views/ShellView.xaml b/Gremlin/MVVM/Views/ShellView.xaml index 1de37f3..f3c9e3b 100644 --- a/Gremlin/MVVM/Views/ShellView.xaml +++ b/Gremlin/MVVM/Views/ShellView.xaml @@ -4,7 +4,16 @@ xmlns:cal="http://www.caliburnproject.org" xmlns:local="clr-namespace:Gremlin.MVVM"> - +