Aufgabe:
In der ... können u.a. Kundendaten verwaltet werden.
Es soll eine Applikation erstellt werden, die die Kundendaten in eine Liste/Tabelle anzeigt und mit der die Kundendaten bearbeitet werden können.
Links soll die Tabelle/Liste mit allen Kunden zu sehen sein und rechts die einzelnen Eingabefelder für einen Kundendatensatz.
Wir links in der Tabelle ein Kundendatensatz ausgewählt, so sind die Daten rechts in den Eingabecontrols zu sehen und können bei Bedarf geändert und gespeichert werden.
Die Kundennummer (Primärschlüssel) darf nicht geändert werden, da dieser von der Datenbank verwaltet wird.
Die Tabelle in der Datenbank heißt "Kunden".
Der Primärschlüssel/Kundennummer heißt "KDNUMMER".
Es müssen nicht alle Tabellenfelder in der Tabelle und rechts zur Bearbeitung angezeigt werden, 10 Felder sollen ausreichend sein.
Die Aufgabe habe ich so gelöst:
MainWindow.xaml:
<Window x:Class="... .MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MLAN_Kunden"
mc:Ignorable="d"
Title="MainWindow" Height="679.747" Width="1001.764" WindowStartupLocation="CenterScreen">
<Grid Background="#FF434751">
<ListBox x:Name="listBox" HorizontalAlignment="Left" Height="550" Margin="27,28,0,0" VerticalAlignment="Top" Width="434" Background="#FF354D93" SelectionChanged="ListBox_SelectionChanged" FontWeight="Bold" Foreground="White"/>
<Label x:Name="labelKundenNummerPK" Content="Kunden Nummer PK:" HorizontalAlignment="Left" Margin="498,28,0,0" VerticalAlignment="Top" Foreground="White" FontWeight="Bold" FontSize="18"/>
<TextBox x:Name="textBoxKundenNummerPK" HorizontalAlignment="Left" Height="24" Margin="504,62,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="462" FontWeight="Bold" IsEnabled="False"/>
<Label x:Name="labelOffeneVorgaenge" Content="Offene Vorgänge:" HorizontalAlignment="Left" Margin="498,83,0,0" VerticalAlignment="Top" Foreground="White" FontWeight="Bold" FontSize="18"/>
<TextBox x:Name="textBoxOffeneVorgaenge" HorizontalAlignment="Left" Height="24" Margin="504,118,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="462" FontWeight="Bold"/>
<Label x:Name="labelKundenBruttopreise" Content="Bruttopreise:" HorizontalAlignment="Left" Margin="498,137,0,0" VerticalAlignment="Top" Foreground="White" FontWeight="Bold" FontSize="18"/>
<TextBox x:Name="textBoxBruttopreise" HorizontalAlignment="Left" Height="24" Margin="504,171,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="462" FontWeight="Bold"/>
<Label x:Name="labelOffenePosten" Content="Offene Posten:" HorizontalAlignment="Left" Margin="498,192,0,0" VerticalAlignment="Top" Foreground="White" FontWeight="Bold" FontSize="18"/>
<TextBox x:Name="textBoxOffenePosten" HorizontalAlignment="Left" Height="24" Margin="504,227,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="462" FontWeight="Bold"/>
<Label x:Name="labelTransportweg" Content="Transportweg:" HorizontalAlignment="Left" Margin="498,247,0,0" VerticalAlignment="Top" Foreground="White" FontWeight="Bold" FontSize="18"/>
<TextBox x:Name="textBoxOffeneTransportweg" HorizontalAlignment="Left" Height="24" Margin="504,282,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="462" FontWeight="Bold"/>
<Label x:Name="labelPreis" Content="Preis:" HorizontalAlignment="Left" Margin="498,302,0,0" VerticalAlignment="Top" Foreground="White" FontWeight="Bold" FontSize="18"/>
<TextBox x:Name="textBoxPreis" HorizontalAlignment="Left" Height="24" Margin="504,338,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="462" FontWeight="Bold"/>
<Label x:Name="labelVersandanschrift_" Content="Versandanschrift:" HorizontalAlignment="Left" Margin="498,358,0,0" VerticalAlignment="Top" Foreground="White" FontWeight="Bold" FontSize="18"/>
<TextBox x:Name="textBoxVersandanschrift" HorizontalAlignment="Left" Height="24" Margin="504,392,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="462" FontWeight="Bold"/>
<Label x:Name="labelVersicherung" Content="Versicherung:" HorizontalAlignment="Left" Margin="498,413,0,0" VerticalAlignment="Top" Foreground="White" FontWeight="Bold" FontSize="18"/>
<TextBox x:Name="textBoxVersicherung" HorizontalAlignment="Left" Height="24" Margin="504,448,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="462" FontWeight="Bold"/>
<Label x:Name="labelBenutzer" Content="Benutzer:" HorizontalAlignment="Left" Margin="498,464,0,0" VerticalAlignment="Top" Foreground="White" FontWeight="Bold" FontSize="18"/>
<TextBox x:Name="textBoxBenutzer" HorizontalAlignment="Left" Height="24" Margin="504,498,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="462" FontWeight="Bold"/>
<Label x:Name="labelZeit" Content="Zeit:" HorizontalAlignment="Left" Margin="498,519,0,0" VerticalAlignment="Top" Foreground="White" FontWeight="Bold" FontSize="18"/>
<TextBox x:Name="textBoxZeit" HorizontalAlignment="Left" Height="24" Margin="504,554,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="462" FontWeight="Bold"/>
<Button x:Name="buttonSpeichern" Content="Speichern" HorizontalAlignment="Left" Margin="260,588,0,0" VerticalAlignment="Top" Width="75" Height="25" Background="#FFCB4141" FontWeight="Bold" Foreground="White" Click="ButtonSpeichern_Click" IsEnabled="False"/>
<Button x:Name="buttonLoeschen" Content="Löschen" HorizontalAlignment="Left" Margin="891,583,0,0" VerticalAlignment="Top" Width="75" Height="25" Background="#FFCB4141" FontWeight="Bold" Foreground="White" Click="ButtonLoeschen_Click"/>
<Button x:Name="buttonBearbeiten" Content="Bearbeiten" HorizontalAlignment="Left" Margin="811,583,0,0" VerticalAlignment="Top" Width="75" Height="25" Background="#FFCB4141" FontWeight="Bold" Foreground="White" Click="ButtonBearbeiten_Click"/>
<Button x:Name="buttonClearListBoxUndTextBoxen" Content="Clear" HorizontalAlignment="Left" Margin="891,613,0,0" VerticalAlignment="Top" Width="75" Height="25" Background="#FFCB4141" FontWeight="Bold" Foreground="White" Click="ButtonClearListBoxUndTextBoxen_Click"/>
<Label x:Name="labelListBoxUndTextBoxenClear" Content="Listbox und Textboxen:" HorizontalAlignment="Left" Margin="605,607,0,0" VerticalAlignment="Top" Foreground="White" FontWeight="Bold" FontSize="18"/>
<Button x:Name="buttonReload" Content="Reload" HorizontalAlignment="Left" Margin="811,613,0,0" VerticalAlignment="Top" Width="75" Height="25" Background="#FFCB4141" FontWeight="Bold" Foreground="White" Click="ButtonReload_Click"/>
<RadioButton x:Name="radioButtonKundenNrPK" Content="Neuen Kunden erstellen" HorizontalAlignment="Left" Margin="27,588,0,0" VerticalAlignment="Top" FontSize="18" FontWeight="Bold" Checked="radioButtonKundenNrPK_Checked" Unchecked="radioButtonKundenNrPK_Unchecked" Foreground="White"/>
</Grid>
</Window>
MainWindow.xaml.cs:
using System.Windows;
using System.Windows.Controls;
namespace ..._Kunden
{
public partial class MainWindow : Window
{
private Datenbank datenbank = new Datenbank();
private Logik logik = new Logik();
public MainWindow()
{
InitializeComponent();
datenbank.KundenLesen(listBox);
}
private void ButtonSpeichern_Click(object sender, RoutedEventArgs e)
{
KundenDaten kundenDaten = new KundenDaten(textBoxKundenNummerPK.Text, textBoxOffeneVorgaenge.Text, textBoxBruttopreise.Text, textBoxOffenePosten.Text, textBoxOffeneTransportweg.Text, textBoxPreis.Text, textBoxVersandanschrift.Text, textBoxVersicherung.Text, textBoxBenutzer.Text, textBoxZeit.Text);
datenbank.NeuenKundenSpeichern(kundenDaten, listBox);
datenbank.KundenLesen(listBox);
}
private void ButtonBearbeiten_Click(object sender, RoutedEventArgs e)
{
KundenDaten kundenDaten = new KundenDaten(textBoxKundenNummerPK.Text, textBoxOffeneVorgaenge.Text, textBoxBruttopreise.Text, textBoxOffenePosten.Text, textBoxOffeneTransportweg.Text, textBoxPreis.Text, textBoxVersandanschrift.Text, textBoxVersicherung.Text, textBoxBenutzer.Text, textBoxZeit.Text);
datenbank.KundenDatenBearbeiten(kundenDaten, listBox);
datenbank.KundenLesen(listBox);
}
private void ButtonLoeschen_Click(object sender, RoutedEventArgs e)
{
datenbank.KundenDatenLoeschen(listBox);
datenbank.KundenLesen(listBox);
}
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (listBox.SelectedIndex != -1)
{
datenbank.LadeDatenInTextboxen(textBoxKundenNummerPK, textBoxOffeneVorgaenge, textBoxBruttopreise, textBoxOffenePosten, textBoxOffeneTransportweg, textBoxPreis, textBoxVersandanschrift, textBoxVersicherung, textBoxBenutzer, textBoxZeit, listBox.SelectedIndex, listBox);
}
}
private void ButtonClearListBoxUndTextBoxen_Click(object sender, RoutedEventArgs e)
{
logik.ListBoxTextBoxenClear(listBox, textBoxKundenNummerPK, textBoxOffeneVorgaenge, textBoxBruttopreise, textBoxOffenePosten, textBoxOffeneTransportweg, textBoxPreis, textBoxVersandanschrift, textBoxVersicherung, textBoxBenutzer, textBoxZeit);
}
private void ButtonReload_Click(object sender, RoutedEventArgs e)
{
logik.ListBoxTextBoxenClear(listBox, textBoxKundenNummerPK, textBoxOffeneVorgaenge, textBoxBruttopreise, textBoxOffenePosten, textBoxOffeneTransportweg, textBoxPreis, textBoxVersandanschrift, textBoxVersicherung, textBoxBenutzer, textBoxZeit);
datenbank.KundenLesen(listBox);
}
private void radioButtonKundenNrPK_Checked(object sender, RoutedEventArgs e)
{
textBoxKundenNummerPK.IsEnabled = true;
buttonSpeichern.IsEnabled = true;
logik.NurTextBoxenClear(textBoxKundenNummerPK, textBoxOffeneVorgaenge, textBoxBruttopreise, textBoxOffenePosten, textBoxOffeneTransportweg, textBoxPreis, textBoxVersandanschrift, textBoxVersicherung, textBoxBenutzer, textBoxZeit);
}
private void radioButtonKundenNrPK_Unchecked(object sender, RoutedEventArgs e)
{
textBoxKundenNummerPK.IsEnabled = false;
buttonSpeichern.IsEnabled = false;
}
}
}
KundenDaten.cs:
namespace ..._Kunden
{
class KundenDaten
{
public string KundenNummerPK { get; set; }
public string OffeneVorgaenge { get; set; }
public string Bruttopreise { get; set; }
public string OffenePosten { get; set; }
public string Transportweg { get; set; }
public string Preis { get; set; }
public string Versandanschrift { get; set; }
public string Versicherung { get; set; }
public string Benutzer { get; set; }
public string Zeit { get; set; }
public KundenDaten(string strKundenNummerPK, string strOffeneVorgaenge, string strBruttopreise, string strOffenePosten, string strTransportweg, string strPreis, string strVersandanschrift, string strVersicherung, string strBenutzer, string strZeit)
{
KundenNummerPK = strKundenNummerPK;
OffeneVorgaenge = strOffeneVorgaenge;
Bruttopreise = strBruttopreise;
OffenePosten = strOffenePosten;
Transportweg = strTransportweg;
Preis = strPreis;
Versandanschrift = strVersandanschrift;
Versicherung = strVersicherung;
Benutzer = strBenutzer;
Zeit = strZeit;
}
public string GetCSVSatz()
{
string s;
s = KundenNummerPK + " / " + OffeneVorgaenge + " / " + Bruttopreise + " / " + OffenePosten + " / " + Transportweg + " / " + Preis + " / " + Versandanschrift + " / " + Versicherung + " / " + Benutzer + " / " + Zeit + "\n";
//s = KundenNummerPK + ";" + OffeneVorgaenge + ";" + Bruttopreise + ";" + OffenePosten + ";" + Transportweg + ";" + Preis + ";" + Versandanschrift + ";" + Versicherung + ";" + Benutzer + ";" + Zeit + ";";
return s;
}
}
}
Datenbank.cs:
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Windows.Controls;
namespace ..._Kunden
{
class Datenbank
{
private readonly string sqlPath = "SERVER=...;DATABASE=...;UID=...;PASSWORD='...';";
private List<KundenDaten> listeKundenDaten = new List<KundenDaten>();
public Datenbank()
{
}
public void KundenLesen(ListBox listBox)
{
using (SqlConnection sqlConnection = new SqlConnection(sqlPath))
{
sqlConnection.Open();
listeKundenDaten.Clear();
listBox.Items.Clear();
using (SqlCommand command = new SqlCommand("SELECT * FROM KUNDEN ORDER BY KDNUMMER", sqlConnection))
{
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
listeKundenDaten.Add(
new KundenDaten(
reader["KDNUMMER"].ToString(), //KundenNummer PK
reader["KDOPVA"].ToString(), //Offene Vorgaenge
reader["KDBRUTTO"].ToString(), //Bruttopreise
reader["KDOPRG"].ToString(), //Offene Posten
reader["KDEMTRWEG"].ToString(), //Transportweg
reader["KDPREISKZ"].ToString(), //Preis
reader["KDVANUMMER"].ToString(), //Versandanschrift
reader["KDVERSICH"].ToString(), //Versicherung
reader["KDWSNAME"].ToString(), //Benutzer
reader["KDWSZEIT"].ToString() //Zeit
));
}
}
}
}
foreach (KundenDaten kundenDaten in listeKundenDaten)
{
listBox.Items.Add(kundenDaten.GetCSVSatz());
}
}
public void NeuenKundenSpeichern(KundenDaten kundenDaten, ListBox listBox)
{
using (SqlConnection sqlConnection = new SqlConnection(sqlPath))
{
sqlConnection.Open();
string sqlAnweisung;
SqlCommand sqlCommand = sqlConnection.CreateCommand();
sqlAnweisung = string.Format("INSERT INTO KUNDEN(KDNUMMER, KDOPVA, KDBRUTTO, KDOPRG, KDEMTRWEG, KDPREISKZ, KDVANUMMER, KDVERSICH, KDWSNAME, KDWSZEIT) VALUES ( '{0}', '{1}', '{2}','{3}', '{4}', '{5}', '{6}','{7}','{8}','{9}');",
kundenDaten.KundenNummerPK, kundenDaten.OffeneVorgaenge.Replace(",", "."), kundenDaten.Bruttopreise, kundenDaten.OffenePosten.Replace(",", "."), kundenDaten.Transportweg, kundenDaten.Preis, kundenDaten.Versandanschrift, kundenDaten.Versicherung, kundenDaten.Benutzer, kundenDaten.Zeit);
sqlCommand.CommandText = sqlAnweisung;
sqlCommand.ExecuteNonQuery();
}
}
public void KundenDatenBearbeiten(KundenDaten kundenDaten, ListBox listBox)
{
using (SqlConnection sqlConnection = new SqlConnection(sqlPath))
{
sqlConnection.Open();
string sqlAnweisung;
SqlCommand sqlCommand = sqlConnection.CreateCommand();
sqlAnweisung = string.Format("UPDATE KUNDEN SET KDNUMMER = '{0}', KDOPVA = '{1}', KDBRUTTO = '{2}', KDOPRG = '{3}', " +
"KDEMTRWEG = '{4}', KDPREISKZ = '{5}', KDVANUMMER = '{6}', KDVERSICH = '{7}', KDWSNAME = '{8}', KDWSZEIT = '{9}' ",
kundenDaten.KundenNummerPK, kundenDaten.OffeneVorgaenge.Replace(",", "."), kundenDaten.Bruttopreise, kundenDaten.OffenePosten.Replace(",", "."), kundenDaten.Transportweg, kundenDaten.Preis, kundenDaten.Versandanschrift, kundenDaten.Versicherung, kundenDaten.Benutzer, kundenDaten.Zeit);
sqlAnweisung += string.Format("WHERE KDNUMMER = '{0}';", kundenDaten.KundenNummerPK);
sqlCommand.CommandText = sqlAnweisung;
sqlCommand.ExecuteNonQuery();
sqlConnection.Close();
}
}
public void KundenDatenLoeschen(ListBox listBox)
{
using (SqlConnection sqlConnection = new SqlConnection(sqlPath))
{
sqlConnection.Open();
SqlCommand sqlCommand = sqlConnection.CreateCommand();
sqlCommand.CommandText = "DELETE FROM KUNDEN "
+ "WHERE KDNUMMER = " + listBox.SelectedIndex.ToString() + ";";
sqlCommand.ExecuteNonQuery();
sqlConnection.Close();
}
}
public void LadeDatenInTextboxen(TextBox textBoxKundenNummerPK, TextBox textBoxOffeneVorgaenge, TextBox textBoxBruttopreise, TextBox textBoxOffenePosten, TextBox textBoxTransportweg, TextBox textBoxPreis, TextBox textBoxVersandanschrift, TextBox textBoxVersicherung, TextBox textBoxBenutzer, TextBox textBoxZeit, int index, ListBox listBox)
{
textBoxKundenNummerPK.Text = listeKundenDaten[index].KundenNummerPK;
textBoxOffeneVorgaenge.Text = listeKundenDaten[index].OffeneVorgaenge;
textBoxBruttopreise.Text = listeKundenDaten[index].Bruttopreise;
textBoxOffenePosten.Text = listeKundenDaten[index].OffenePosten;
textBoxTransportweg.Text = listeKundenDaten[index].Transportweg;
textBoxPreis.Text = listeKundenDaten[index].Preis;
textBoxVersandanschrift.Text = listeKundenDaten[index].Versandanschrift;
textBoxVersicherung.Text = listeKundenDaten[index].Versicherung;
textBoxBenutzer.Text = listeKundenDaten[index].Benutzer;
textBoxZeit.Text = listeKundenDaten[index].Zeit;
}
}
}
Logik.cs:
using System.Windows.Controls;
namespace ..._Kunden
{
class Logik
{
public Logik()
{
}
public void ListBoxTextBoxenClear(ListBox listBox, TextBox textBoxKundenNummerPK, TextBox textBoxOffeneVorgaenge, TextBox textBoxBruttopreise, TextBox textBoxOffenePosten, TextBox textBoxTransportweg, TextBox textBoxPreis, TextBox textBoxVersandanschrift, TextBox textBoxVersicherung, TextBox textBoxBenutzer, TextBox textBoxZeit)
{
listBox.Items.Clear();
textBoxKundenNummerPK.Text = "";
textBoxOffeneVorgaenge.Text = "";
textBoxBruttopreise.Text = "";
textBoxOffenePosten.Text = "";
textBoxTransportweg.Text = "";
textBoxPreis.Text = "";
textBoxVersandanschrift.Text = "";
textBoxVersicherung.Text = "";
textBoxBenutzer.Text = "";
textBoxZeit.Text = "";
}
public void NurTextBoxenClear(TextBox textBoxKundenNummerPK, TextBox textBoxOffeneVorgaenge, TextBox textBoxBruttopreise, TextBox textBoxOffenePosten, TextBox textBoxTransportweg, TextBox textBoxPreis, TextBox textBoxVersandanschrift, TextBox textBoxVersicherung, TextBox textBoxBenutzer, TextBox textBoxZeit)
{
textBoxKundenNummerPK.Text = "";
textBoxOffeneVorgaenge.Text = "";
textBoxBruttopreise.Text = "";
textBoxOffenePosten.Text = "";
textBoxTransportweg.Text = "";
textBoxPreis.Text = "";
textBoxVersandanschrift.Text = "";
textBoxVersicherung.Text = "";
textBoxBenutzer.Text = "";
textBoxZeit.Text = "";
}
}
}
Das Programm funktioniert, aber jetzt habe ich folgende extra Aufgabe bekommen:
Datenbindung Model -> ViewModel -> View
Ziel: Es sollen sämtliche direkten Verweise auf die Controls (ListBox, TextBox, etc.) in den Klassen entfernt werden und die Zuweisung der einzelnen Kundendaten durch Datenbindung der Properties erreicht werden.
Beispiel: statt textBoxName.Text = "Gerd" wird geschrieben kunde.Name = "Gerd". kunde ist hier das Objekt der Model-Klasse "Kunde" und besitzt die Property "Name". Diese Property wird an die Text-Property der TextBox in der View (XAML) gebunden.
Der ListBox werden nicht einzeln die Items durch die hinzugefügt (listBox.Items.Add(...)), sondern an eine List von Kunden gebunden.
Benötigt wird die Model-Klasse für einen bzw. die Kunden (hier schon vorhanden in KundenDaten.cs).
Eine generische List zur Bindung der aller Kundendaten an die ListBox.
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Ich mache seit einem Monat eine Ausbildung zum Fachinformatiker für Anwendungsentwicklung. Mir fehlen noch viele Grundkenntnisse und die extra Aufgabe kriege ich bis jetzt nicht wirklich gelöst.
Ich habe folgendes Beispiel als Hilfe bekommen: https://openbook.rheinwerk-verlag.de/visual_csharp_2012/1997_28_005.html
Jetzt habe ich eine ViewModelKlasse erstellt und die anderen Klassen verändert bzw. erstmal sehr viel auskommentiert.
Die Klassen sehen jetzt so aus:
MainWindow.xaml (Ich habe erstmal aus Testzwecken Daten an die Textboxen gebunden. Richtig wäre es, sie an die Listbox zu binden)
<Window x:Class="..._Kunden.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MLAN_Kunden"
mc:Ignorable="d"
Title="MainWindow" Height="679.747" Width="1001.764" WindowStartupLocation="CenterScreen">
<Window.DataContext>
<local:KundenDaten/>
</Window.DataContext>
<Grid Background="#FF434751">
<ListBox x:Name="listBox" HorizontalAlignment="Left" Height="550" Margin="27,28,0,0" VerticalAlignment="Top" Width="434" Background="#FF354D93" SelectionChanged="ListBox_SelectionChanged" FontWeight="Bold" Foreground="White" ItemsSource="{Binding KundenNummerPK}"/>
<Label x:Name="labelKundenNummerPK" Content="Kunden Nummer PK:" HorizontalAlignment="Left" Margin="498,28,0,0" VerticalAlignment="Top" Foreground="White" FontWeight="Bold" FontSize="18"/>
<TextBox x:Name="textBoxKundenNummerPK" HorizontalAlignment="Left" Height="24" Margin="504,62,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="462" FontWeight="Bold" IsEnabled="False" Text="{Binding KundenNummerPK}"/>
<Label x:Name="labelOffeneVorgaenge" Content="Offene Vorgänge:" HorizontalAlignment="Left" Margin="498,83,0,0" VerticalAlignment="Top" Foreground="White" FontWeight="Bold" FontSize="18"/>
<TextBox x:Name="textBoxOffeneVorgaenge" HorizontalAlignment="Left" Height="24" Margin="504,118,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="462" FontWeight="Bold" Text="{Binding OffeneVorgaenge}"/>
<Label x:Name="labelKundenBruttopreise" Content="Bruttopreise:" HorizontalAlignment="Left" Margin="498,137,0,0" VerticalAlignment="Top" Foreground="White" FontWeight="Bold" FontSize="18"/>
<TextBox x:Name="textBoxBruttopreise" HorizontalAlignment="Left" Height="24" Margin="504,171,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="462" FontWeight="Bold" Text="{Binding Bruttopreise}"/>
<Label x:Name="labelOffenePosten" Content="Offene Posten:" HorizontalAlignment="Left" Margin="498,192,0,0" VerticalAlignment="Top" Foreground="White" FontWeight="Bold" FontSize="18"/>
<TextBox x:Name="textBoxOffenePosten" HorizontalAlignment="Left" Height="24" Margin="504,227,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="462" FontWeight="Bold" Text="{Binding OffenePosten}"/>
<Label x:Name="labelTransportweg" Content="Transportweg:" HorizontalAlignment="Left" Margin="498,247,0,0" VerticalAlignment="Top" Foreground="White" FontWeight="Bold" FontSize="18"/>
<TextBox x:Name="textBoxOffeneTransportweg" HorizontalAlignment="Left" Height="24" Margin="504,282,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="462" FontWeight="Bold" Text="{Binding Transportweg}"/>
<Label x:Name="labelPreis" Content="Preis:" HorizontalAlignment="Left" Margin="498,302,0,0" VerticalAlignment="Top" Foreground="White" FontWeight="Bold" FontSize="18"/>
<TextBox x:Name="textBoxPreis" HorizontalAlignment="Left" Height="24" Margin="504,338,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="462" FontWeight="Bold" Text="{Binding Preis}"/>
<Label x:Name="labelVersandanschrift_" Content="Versandanschrift:" HorizontalAlignment="Left" Margin="498,358,0,0" VerticalAlignment="Top" Foreground="White" FontWeight="Bold" FontSize="18"/>
<TextBox x:Name="textBoxVersandanschrift" HorizontalAlignment="Left" Height="24" Margin="504,392,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="462" FontWeight="Bold" Text="{Binding Versandanschrift}"/>
<Label x:Name="labelVersicherung" Content="Versicherung:" HorizontalAlignment="Left" Margin="498,413,0,0" VerticalAlignment="Top" Foreground="White" FontWeight="Bold" FontSize="18"/>
<TextBox x:Name="textBoxVersicherung" HorizontalAlignment="Left" Height="24" Margin="504,448,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="462" FontWeight="Bold" Text="{Binding Versicherung}"/>
<Label x:Name="labelBenutzer" Content="Benutzer:" HorizontalAlignment="Left" Margin="498,464,0,0" VerticalAlignment="Top" Foreground="White" FontWeight="Bold" FontSize="18"/>
<TextBox x:Name="textBoxBenutzer" HorizontalAlignment="Left" Height="24" Margin="504,498,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="462" FontWeight="Bold" Text="{Binding Benutzer}"/>
<Label x:Name="labelZeit" Content="Zeit:" HorizontalAlignment="Left" Margin="498,519,0,0" VerticalAlignment="Top" Foreground="White" FontWeight="Bold" FontSize="18"/>
<TextBox x:Name="textBoxZeit" HorizontalAlignment="Left" Height="24" Margin="504,554,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="462" FontWeight="Bold" Text="{Binding Zeit}"/>
<Button x:Name="buttonSpeichern" Content="Speichern" HorizontalAlignment="Left" Margin="260,588,0,0" VerticalAlignment="Top" Width="75" Height="25" Background="#FFCB4141" FontWeight="Bold" Foreground="White" Click="ButtonSpeichern_Click" IsEnabled="False"/>
<Button x:Name="buttonLoeschen" Content="Löschen" HorizontalAlignment="Left" Margin="891,583,0,0" VerticalAlignment="Top" Width="75" Height="25" Background="#FFCB4141" FontWeight="Bold" Foreground="White" Click="ButtonLoeschen_Click"/>
<Button x:Name="buttonBearbeiten" Content="Bearbeiten" HorizontalAlignment="Left" Margin="811,583,0,0" VerticalAlignment="Top" Width="75" Height="25" Background="#FFCB4141" FontWeight="Bold" Foreground="White" Click="ButtonBearbeiten_Click"/>
<Button x:Name="buttonClearListBoxUndTextBoxen" Content="Clear" HorizontalAlignment="Left" Margin="891,613,0,0" VerticalAlignment="Top" Width="75" Height="25" Background="#FFCB4141" FontWeight="Bold" Foreground="White" Click="ButtonClearListBoxUndTextBoxen_Click"/>
<Label x:Name="labelListBoxUndTextBoxenClear" Content="Listbox und Textboxen:" HorizontalAlignment="Left" Margin="605,607,0,0" VerticalAlignment="Top" Foreground="White" FontWeight="Bold" FontSize="18"/>
<Button x:Name="buttonReload" Content="Reload" HorizontalAlignment="Left" Margin="811,613,0,0" VerticalAlignment="Top" Width="75" Height="25" Background="#FFCB4141" FontWeight="Bold" Foreground="White" Click="ButtonReload_Click"/>
<RadioButton x:Name="radioButtonKundenNrPK" Content="Neuen Kunden erstellen" HorizontalAlignment="Left" Margin="27,588,0,0" VerticalAlignment="Top" FontSize="18" FontWeight="Bold" Checked="radioButtonKundenNrPK_Checked" Unchecked="radioButtonKundenNrPK_Unchecked" Foreground="White"/>
</Grid>
</Window>
MainWindow.xaml.cs:(Eigentlich nur fast alles auskommentiert. Das ist neu --> this.DataContext = new MainWindowViewModel(KundenDaten.CreateKundenDaten()); )
using System.Windows;
using System.Windows.Controls;
namespace ..._Kunden
{
public partial class MainWindow : Window
{
private Datenbank datenbank = new Datenbank();
private Logik logik = new Logik();
public MainWindow()
{
InitializeComponent();
//datenbank.KundenLesen(listBox);
this.DataContext = new MainWindowViewModel(KundenDaten.CreateKundenDaten());
}
private void ButtonSpeichern_Click(object sender, RoutedEventArgs e)
{
/*
KundenDaten kundenDaten = new KundenDaten(textBoxKundenNummerPK.Text, textBoxOffeneVorgaenge.Text, textBoxBruttopreise.Text, textBoxOffenePosten.Text, textBoxOffeneTransportweg.Text, textBoxPreis.Text, textBoxVersandanschrift.Text, textBoxVersicherung.Text, textBoxBenutzer.Text, textBoxZeit.Text);
datenbank.NeuenKundenSpeichern(kundenDaten, listBox);
datenbank.KundenLesen(listBox);
*/
}
private void ButtonBearbeiten_Click(object sender, RoutedEventArgs e)
{
/*
KundenDaten kundenDaten = new KundenDaten(textBoxKundenNummerPK.Text, textBoxOffeneVorgaenge.Text, textBoxBruttopreise.Text, textBoxOffenePosten.Text, textBoxOffeneTransportweg.Text, textBoxPreis.Text, textBoxVersandanschrift.Text, textBoxVersicherung.Text, textBoxBenutzer.Text, textBoxZeit.Text);
datenbank.KundenDatenBearbeiten(kundenDaten, listBox);
datenbank.KundenLesen(listBox);
*/
}
private void ButtonLoeschen_Click(object sender, RoutedEventArgs e)
{
/*
datenbank.KundenDatenLoeschen(listBox);
datenbank.KundenLesen(listBox);
*/
}
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
/*
if (listBox.SelectedIndex != -1)
{
datenbank.LadeDatenInTextboxen(textBoxKundenNummerPK, textBoxOffeneVorgaenge, textBoxBruttopreise, textBoxOffenePosten, textBoxOffeneTransportweg, textBoxPreis, textBoxVersandanschrift, textBoxVersicherung, textBoxBenutzer, textBoxZeit, listBox.SelectedIndex, listBox);
}
*/
}
private void ButtonClearListBoxUndTextBoxen_Click(object sender, RoutedEventArgs e)
{
/*
logik.ListBoxTextBoxenClear(listBox, textBoxKundenNummerPK, textBoxOffeneVorgaenge, textBoxBruttopreise, textBoxOffenePosten, textBoxOffeneTransportweg, textBoxPreis, textBoxVersandanschrift, textBoxVersicherung, textBoxBenutzer, textBoxZeit);
*/
}
private void ButtonReload_Click(object sender, RoutedEventArgs e)
{
/*
logik.ListBoxTextBoxenClear(listBox, textBoxKundenNummerPK, textBoxOffeneVorgaenge, textBoxBruttopreise, textBoxOffenePosten, textBoxOffeneTransportweg, textBoxPreis, textBoxVersandanschrift, textBoxVersicherung, textBoxBenutzer, textBoxZeit);
datenbank.KundenLesen(listBox);
*/
}
private void radioButtonKundenNrPK_Checked(object sender, RoutedEventArgs e)
{
/*
textBoxKundenNummerPK.IsEnabled = true;
buttonSpeichern.IsEnabled = true;
logik.NurTextBoxenClear(textBoxKundenNummerPK, textBoxOffeneVorgaenge, textBoxBruttopreise, textBoxOffenePosten, textBoxOffeneTransportweg, textBoxPreis, textBoxVersandanschrift, textBoxVersicherung, textBoxBenutzer, textBoxZeit);
*/
}
private void radioButtonKundenNrPK_Unchecked(object sender, RoutedEventArgs e)
{
/*
textBoxKundenNummerPK.IsEnabled = false;
buttonSpeichern.IsEnabled = false;
*/
}
}
}
KundenDaten.cs: (Hier habe ich nur den Konstrukter entfernt und die Mehtode CreateKundenDaten() erstellt.)
using System.ComponentModel;
namespace ..._Kunden
{
public class KundenDaten
{
public string KundenNummerPK { get; set; }
public string OffeneVorgaenge { get; set; }
public string Bruttopreise { get; set; }
public string OffenePosten { get; set; }
public string Transportweg { get; set; }
public string Preis { get; set; }
public string Versandanschrift { get; set; }
public string Versicherung { get; set; }
public string Benutzer { get; set; }
public string Zeit { get; set; }
/*
public KundenDaten(string strKundenNummerPK, string strOffeneVorgaenge, string strBruttopreise, string strOffenePosten, string strTransportweg, string strPreis, string strVersandanschrift, string strVersicherung, string strBenutzer, string strZeit)
{
KundenNummerPK = strKundenNummerPK;
OffeneVorgaenge = strOffeneVorgaenge;
Bruttopreise = strBruttopreise;
OffenePosten = strOffenePosten;
Transportweg = strTransportweg;
Preis = strPreis;
Versandanschrift = strVersandanschrift;
Versicherung = strVersicherung;
Benutzer = strBenutzer;
Zeit = strZeit;
}
*/
public string GetCSVSatz()
{
string s;
s = KundenNummerPK + " / " + OffeneVorgaenge + " / " + Bruttopreise + " / " + OffenePosten + " / " + Transportweg + " / " + Preis + " / " + Versandanschrift + " / " + Versicherung + " / " + Benutzer + " / " + Zeit + "\n";
//s = KundenNummerPK + ";" + OffeneVorgaenge + ";" + Bruttopreise + ";" + OffenePosten + ";" + Transportweg + ";" + Preis + ";" + Versandanschrift + ";" + Versicherung + ";" + Benutzer + ";" + Zeit + ";";
return s;
}
public static KundenDaten CreateKundenDaten()
{
return new KundenDaten { KundenNummerPK = "1", OffeneVorgaenge = "22", Bruttopreise = "23", OffenePosten = "24", Transportweg = "25", Preis = "26", Versandanschrift = "27", Versicherung = "28", Benutzer = "Marvin", Zeit = "29" };
}
}
}
MainWindowViewModel.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ..._Kunden
{
public class MainWindowViewModel : INotifyPropertyChanged
{
public KundenDaten _Model;
// Konstruktor
public MainWindowViewModel(KundenDaten model)
{
_Model = model;
_KundenNummerPK = _Model.KundenNummerPK;
_OffeneVorgaenge = _Model.OffeneVorgaenge;
_Bruttopreise = _Model.Bruttopreise;
_OffenePosten = _Model.OffenePosten;
_Transportweg = _Model.Transportweg;
_Preis = _Model.Preis;
_Versandanschrift = _Model.Versandanschrift;
_Versicherung = _Model.Versicherung;
_Benutzer = _Model.Benutzer;
_Zeit = _Model.Zeit;
}
// Schnittstellen-Ereignis
public event PropertyChangedEventHandler PropertyChanged;
protected internal void OnPropertyChanged(string propertyname)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyname));
}
// Eigenschaften
private string _KundenNummerPK;
public string KundenNummerPK
{
get { return _KundenNummerPK; }
set
{
if (_KundenNummerPK == value) return;
_KundenNummerPK = value;
OnPropertyChanged("KundenNummerPK");
}
}
private string _OffeneVorgaenge;
public string OffeneVorgaenge
{
get { return _OffeneVorgaenge; }
set
{
if (_OffeneVorgaenge == value) return;
_OffeneVorgaenge = value;
OnPropertyChanged("OffeneVorgaenge");
}
}
private string _Bruttopreise;
public string Bruttopreise
{
get { return _Bruttopreise; }
set
{
if (_Bruttopreise == value) return;
_Bruttopreise = value;
OnPropertyChanged("Bruttopreise");
}
}
private string _OffenePosten;
public string OffenePosten
{
get { return _OffenePosten; }
set
{
if (_OffenePosten == value) return;
_OffenePosten = value;
OnPropertyChanged("OffenePosten");
}
}
private string _Transportweg;
public string Transportweg
{
get { return _Transportweg; }
set
{
if (_Transportweg == value) return;
_Transportweg = value;
OnPropertyChanged("Transportweg");
}
}
private string _Preis;
public string Preis
{
get { return _Preis; }
set
{
if (_Preis == value) return;
_Preis = value;
OnPropertyChanged("Preis");
}
}
private string _Versandanschrift;
public string Versandanschrift
{
get { return _Versandanschrift; }
set
{
if (_Versandanschrift == value) return;
_Versandanschrift = value;
OnPropertyChanged("Versandanschrift");
}
}
private string _Versicherung;
public string Versicherung
{
get { return _Versicherung; }
set
{
if (_Versicherung == value) return;
_Versicherung = value;
OnPropertyChanged("Versicherung");
}
}
private string _Benutzer;
public string Benutzer
{
get { return _Benutzer; }
set
{
if (_Benutzer == value) return;
_Benutzer = value;
OnPropertyChanged("Benutzer");
}
}
private string _Zeit;
public string Zeit
{
get { return _Zeit; }
set
{
if (_Zeit == value) return;
_Zeit = value;
OnPropertyChanged("Zeit");
}
}
}
}
Datenbank.cs:(Eigentlich fast alles auskommentiert)
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Windows.Controls;
namespace ..._Kunden
{
class Datenbank
{
private readonly string sqlPath = "SERVER=...;DATABASE=...;UID=...;PASSWORD='...';";
private List<KundenDaten> listeKundenDaten = new List<KundenDaten>();
public Datenbank()
{
}
/*
public void KundenLesen(ListBox listBox)
{
using (SqlConnection sqlConnection = new SqlConnection(sqlPath))
{
sqlConnection.Open();
listeKundenDaten.Clear();
listBox.Items.Clear();
using (SqlCommand command = new SqlCommand("SELECT * FROM KUNDEN ORDER BY KDNUMMER", sqlConnection))
{
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
listeKundenDaten.Add(
new KundenDaten(
reader["KDNUMMER"].ToString(), //KundenNummer PK
reader["KDOPVA"].ToString(), //Offene Vorgaenge
reader["KDBRUTTO"].ToString(), //Bruttopreise
reader["KDOPRG"].ToString(), //Offene Posten
reader["KDEMTRWEG"].ToString(), //Transportweg
reader["KDPREISKZ"].ToString(), //Preis
reader["KDVANUMMER"].ToString(), //Versandanschrift
reader["KDVERSICH"].ToString(), //Versicherung
reader["KDWSNAME"].ToString(), //Benutzer
reader["KDWSZEIT"].ToString() //Zeit
));
}
}
}
}
foreach (KundenDaten kundenDaten in listeKundenDaten)
{
listBox.Items.Add(kundenDaten.GetCSVSatz());
}
}
public void NeuenKundenSpeichern(KundenDaten kundenDaten, ListBox listBox)
{
using (SqlConnection sqlConnection = new SqlConnection(sqlPath))
{
sqlConnection.Open();
string sqlAnweisung;
SqlCommand sqlCommand = sqlConnection.CreateCommand();
sqlAnweisung = string.Format("INSERT INTO KUNDEN(KDNUMMER, KDOPVA, KDBRUTTO, KDOPRG, KDEMTRWEG, KDPREISKZ, KDVANUMMER, KDVERSICH, KDWSNAME, KDWSZEIT) VALUES ( '{0}', '{1}', '{2}','{3}', '{4}', '{5}', '{6}','{7}','{8}','{9}');",
kundenDaten.KundenNummerPK, kundenDaten.OffeneVorgaenge.Replace(",", "."), kundenDaten.Bruttopreise, kundenDaten.OffenePosten.Replace(",", "."), kundenDaten.Transportweg, kundenDaten.Preis, kundenDaten.Versandanschrift, kundenDaten.Versicherung, kundenDaten.Benutzer, kundenDaten.Zeit);
sqlCommand.CommandText = sqlAnweisung;
sqlCommand.ExecuteNonQuery();
}
}
public void KundenDatenBearbeiten(KundenDaten kundenDaten, ListBox listBox)
{
using (SqlConnection sqlConnection = new SqlConnection(sqlPath))
{
sqlConnection.Open();
string sqlAnweisung;
SqlCommand sqlCommand = sqlConnection.CreateCommand();
sqlAnweisung = string.Format("UPDATE KUNDEN SET KDNUMMER = '{0}', KDOPVA = '{1}', KDBRUTTO = '{2}', KDOPRG = '{3}', " +
"KDEMTRWEG = '{4}', KDPREISKZ = '{5}', KDVANUMMER = '{6}', KDVERSICH = '{7}', KDWSNAME = '{8}', KDWSZEIT = '{9}' ",
kundenDaten.KundenNummerPK, kundenDaten.OffeneVorgaenge.Replace(",", "."), kundenDaten.Bruttopreise, kundenDaten.OffenePosten.Replace(",", "."), kundenDaten.Transportweg, kundenDaten.Preis, kundenDaten.Versandanschrift, kundenDaten.Versicherung, kundenDaten.Benutzer, kundenDaten.Zeit);
sqlAnweisung += string.Format("WHERE KDNUMMER = '{0}';", kundenDaten.KundenNummerPK);
sqlCommand.CommandText = sqlAnweisung;
sqlCommand.ExecuteNonQuery();
sqlConnection.Close();
}
}
public void KundenDatenLoeschen(ListBox listBox)
{
using (SqlConnection sqlConnection = new SqlConnection(sqlPath))
{
sqlConnection.Open();
SqlCommand sqlCommand = sqlConnection.CreateCommand();
sqlCommand.CommandText = "DELETE FROM KUNDEN "
+ "WHERE KDNUMMER = " + listBox.SelectedIndex.ToString() + ";";
sqlCommand.ExecuteNonQuery();
sqlConnection.Close();
}
}
public void LadeDatenInTextboxen(TextBox textBoxKundenNummerPK, TextBox textBoxOffeneVorgaenge, TextBox textBoxBruttopreise, TextBox textBoxOffenePosten, TextBox textBoxTransportweg, TextBox textBoxPreis, TextBox textBoxVersandanschrift, TextBox textBoxVersicherung, TextBox textBoxBenutzer, TextBox textBoxZeit, int index, ListBox listBox)
{
//Aufgabe: Parameter als objekt übergeben! Aber ich weiß nicht wie..
textBoxKundenNummerPK.Text = listeKundenDaten[index].KundenNummerPK;
textBoxOffeneVorgaenge.Text = listeKundenDaten[index].OffeneVorgaenge;
textBoxBruttopreise.Text = listeKundenDaten[index].Bruttopreise;
textBoxOffenePosten.Text = listeKundenDaten[index].OffenePosten;
textBoxTransportweg.Text = listeKundenDaten[index].Transportweg;
textBoxPreis.Text = listeKundenDaten[index].Preis;
textBoxVersandanschrift.Text = listeKundenDaten[index].Versandanschrift;
textBoxVersicherung.Text = listeKundenDaten[index].Versicherung;
textBoxBenutzer.Text = listeKundenDaten[index].Benutzer;
textBoxZeit.Text = listeKundenDaten[index].Zeit;
}
*/
/*
public void KundenLesen(ListBox listBox)
{
using (SqlConnection sqlConnection = new SqlConnection(sqlPath))
{
sqlConnection.Open();
listeKundenDaten.Clear();
listBox.Items.Clear();
using (SqlCommand command = new SqlCommand("SELECT * FROM KUNDEN ORDER BY KDNUMMER", sqlConnection))
{
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
listeKundenDaten.Add(
new KundenDaten(
reader["KDNUMMER"].ToString(), //KundenNummer PK
reader["KDOPVA"].ToString(), //Offene Vorgaenge
reader["KDBRUTTO"].ToString(), //Bruttopreise
reader["KDOPRG"].ToString(), //Offene Posten
reader["KDEMTRWEG"].ToString(), //Transportweg
reader["KDPREISKZ"].ToString(), //Preis
reader["KDVANUMMER"].ToString(), //Versandanschrift
reader["KDVERSICH"].ToString(), //Versicherung
reader["KDWSNAME"].ToString(), //Benutzer
reader["KDWSZEIT"].ToString() //Zeit
));
}
}
}
}
foreach (KundenDaten kundenDaten in listeKundenDaten)
{
listBox.Items.Add(kundenDaten.GetCSVSatz());
}
}
*/
}
}
Das ist halt mein erstes Projekt, in dem ich mit einem Viewmodel arbeiten muss. Ich habe mir viele Tutorials angeguckt und durchgelesen, aber kriege mein Projekt nicht wirklich angepasst.
Meine Frage: Seht ihr erste grundlegende Fehler in der erstellung des Viewmodels bzw. bei der Änderung der KundenDaten.cs? Wie soll ich am besten vorgehen als nächstes? Ich steh irgendwie total auf dem Schlauch.
Liebe Grüße und eine gute Nacht
Balboa44