Mvvm Pattern erweitert

This commit is contained in:
Husky
2020-02-21 08:50:00 +01:00
parent 183646b4da
commit 5798fc6108
7 changed files with 51 additions and 52 deletions

View File

@@ -6,8 +6,13 @@
xmlns:local="clr-namespace:KanSan"
xmlns:syncfusion="http://schemas.syncfusion.com/wpf" x:Class="KanSan.MainWindow"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
Title="MainWindow" Height="450" Width="800" WindowStartupLocation="CenterScreen" WindowState="Maximized">
<Grid>
<ContentControl Name="ContentController" Content="ContentControl" HorizontalAlignment="Left" Height="402" Margin="226,22,0,0" VerticalAlignment="Top" Width="564"/>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<ContentControl Grid.Column="1" Name="ContentController" Content="ContentControl" HorizontalAlignment="Left" Height="402" Margin="226,22,0,0" VerticalAlignment="Top" Width="564"/>
</Grid>
</Window>

View File

@@ -29,10 +29,8 @@ namespace KanSan
InitializeComponent();
this.Title = ProgrammHashVersion.GIT_HASH;
UnitOfWork unitOfWork = new UnitOfWork(new KanSanContext());
var d = unitOfWork.KundenRepository.Get().First();
UI.UCKundeEdit uCKundeEdit = new UI.UCKundeEdit(d);
UI.UCKundeEdit uCKundeEdit = new UI.UCKundeEdit();
ContentController.Content = uCKundeEdit;
}

View File

@@ -23,40 +23,15 @@ namespace KanSan.UI
/// </summary>
public partial class WindowBaustelleEdit : Window
{
private UnitOfWork unitOfWork = new UnitOfWork(new KanSanContext());
private Baustelle bs = null;
public WindowBaustelleEdit(Baustelle baustelle)
public WindowBaustelleEdit()
{
InitializeComponent();
if (baustelle == null) throw new ArgumentNullException("baustelle");
bs = baustelle;
BaustelleViewModel model = new BaustelleViewModel(bs);
model.PropertyChanged += Model_PropertyChanged;
this.DataContext = model;
this.DataContext = new BaustelleViewModel();
}
private void Model_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
BaustelleViewModel baustelleViewModel = (BaustelleViewModel)sender;
bs.Ort = baustelleViewModel.Ort;
bs.Projektnummer = baustelleViewModel.Projektnummer;
bs.Strasse = baustelleViewModel.Strasse;
unitOfWork.BaustellenRepository.Update(bs);
//Debugger.Break();
}
private void Window_Closed(object sender, EventArgs e)
{
unitOfWork.Commit();
}
}
}

View File

@@ -13,6 +13,7 @@
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
@@ -25,6 +26,8 @@
<Label Grid.Column="0" Grid.Row="3">Plz</Label>
<Label Grid.Column="0" Grid.Row="4">Ort</Label>
<Button Name="Speichern" Content="Speichern" Grid.Row="5" Grid.ColumnSpan="2" Click="Speichern_Click" />
<TextBox Grid.Column="1" Grid.Row="0" Text="{Binding Vorname}" />
<TextBox Grid.Column="1" Grid.Row="1" Text="{Binding Nachname}" />
<TextBox Grid.Column="1" Grid.Row="2" Text="{Binding Strasse}" />

View File

@@ -3,6 +3,7 @@ using KanSan.Base.Models;
using KanSan.ViewModel;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Windows;
using System.Windows.Controls;
@@ -21,24 +22,20 @@ namespace KanSan.UI
/// </summary>
public partial class UCKundeEdit : UserControl
{
private Kunde kunde = null;
private UnitOfWork unitOfWork = null;
public UCKundeEdit(Kunde kunde)
//private Kunde kunde = null;
//private UnitOfWork unitOfWork = null;
public UCKundeEdit()
{
InitializeComponent();
this.kunde = kunde;
unitOfWork = new UnitOfWork(new KanSanContext());
KundeViewModel kundeViewModel = new KundeViewModel(kunde);
kundeViewModel.PropertyChanged += KundeViewModel_PropertyChanged;
this.DataContext = kundeViewModel;
this.DataContext = new KundeViewModel();
}
private void KundeViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
private void Speichern_Click(object sender, RoutedEventArgs e)
{
throw new NotImplementedException();
((KundeViewModel)DataContext).Speichern();
}
}
}

View File

@@ -1,7 +1,10 @@
using KanSan.Base.Models;
using KanSan.Base;
using KanSan.Base.Interfaces;
using KanSan.Base.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
@@ -10,7 +13,7 @@ namespace KanSan.ViewModel
class BaustelleViewModel : PropertyChangedClass,INotifyPropertyChanged
{
private Baustelle _baustelle;
IUnitOfWork unitOfWork = new UnitOfWork(new KanSanContext());
private string ort;
private string strasse;
@@ -57,9 +60,9 @@ namespace KanSan.ViewModel
public BaustelleViewModel(Baustelle baustelle)
public BaustelleViewModel()
{
_baustelle = baustelle;
_baustelle = unitOfWork.BaustellenRepository.Get().First();
ort = _baustelle.Ort;
strasse = _baustelle.Strasse;
projektnummer = _baustelle.Projektnummer;

View File

@@ -1,13 +1,18 @@
using KanSan.Base.Models;
using KanSan.Base;
using KanSan.Base.Interfaces;
using KanSan.Base.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
namespace KanSan.ViewModel
{
class KundeViewModel : PropertyChangedClass, INotifyPropertyChanged
{
IUnitOfWork unitOfWork = new UnitOfWork(new KanSanContext());
private Kunde _kunde;
private string vorname;
private string nachname;
@@ -83,14 +88,27 @@ namespace KanSan.ViewModel
}
#endregion
public KundeViewModel(Kunde kunde)
public KundeViewModel()
{
_kunde = kunde;
_kunde = unitOfWork.KundenRepository.Get().First();
vorname = _kunde.Vorname;
nachname = _kunde.Nachname;
strasse = _kunde.Strasse;
plz = _kunde.PLZ;
ort = _kunde.Ort;
}
public void Speichern()
{
_kunde.Vorname = Vorname;
_kunde.Nachname = Nachname;
_kunde.Strasse = Strasse;
_kunde.Ort = Ort;
_kunde.PLZ = PLZ;
unitOfWork.KundenRepository.Update(_kunde);
unitOfWork.Commit();
}
}
}