I am currently trying to create an WPF application in VS 2019 .Net5.0. Unfortunately I could not solve all the problem, because I am beginner with WPF and C#. On the internet I could not find a solution to my problem either.
What I want to do:
- Open a UserControl1 in the Grid.Column="1" in the main window with a button "User". There all users should be read out of the database and listed in the ListView.
In the "UserControl1" I have defined the ListView and 3 buttons:
About button "Add" a window "Window1" should be opened. There I can enter "Firstname" and "Lastname" and save it with the button "Save". The ListView should update itself automatically.
With the button "Edit" the window "Window1" should be opened again with the content of the selected item. There I can edit the content and save it again.
Via button "Delete" the content of the selected item should be deleted.
The ListView and database should be updated each time after "Add, Edit or Delete".
The XAML code for MainWindow:
<Window x:Class="Add_Edit_Delete.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:Add_Edit_Delete"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="70"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="0" Orientation="Vertical" VerticalAlignment="Top" >
<Button Height="50" Width="50" Content="User" Margin="10" />
<Button Height="50" Width="50" Content="Pub" Margin="10"/>
</StackPanel>
</Grid>
</Window>
The XAML code for UserControl1:
<UserControl x:Class="Add_Edit_Delete.Usercontrol.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Add_Edit_Delete.Usercontrol"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800" Background="White">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<ListView Grid.Column="0">
<ListView.View>
<GridView>
<GridViewColumn Header="Firstname" x:Name="Firstname" Width="200">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Identifier}" TextWrapping="Wrap"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Lastname" x:Name="Lastname" Width="200">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" TextWrapping="Wrap"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
<StackPanel Grid.Column="1" Orientation="Horizontal" VerticalAlignment="Top" Margin="20,0,0,0">
<Button Height="25" Width="50" Content="Add" Margin="10" Click="Add_Click"/>
<Button Height="25" Width="50" Content="Edit" Margin="10" Click="Eddit_Click"/>
<Button Height="25" Width="50" Content="Delete" Margin="10" Click="Delete_Click" />
</StackPanel>
</Grid>
</UserControl>
The XAML code for Window1:
<Window x:Class="Add_Edit_Delete.ucWindow.Window1"
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:Add_Edit_Delete.ucWindow"
mc:Ignorable="d"
Title="Window1" Height="450" Width="400">
<Grid>
<StackPanel>
<StackPanel Orientation="Horizontal" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="0,0,0,10">
<Label Content="Firstname:" Width="120" />
<TextBox x:Name="tbFirstname" Height="25" Width="230" Margin="0,0,5,0"/>
</StackPanel>
<StackPanel Orientation="Horizontal" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="0,0,0,10">
<Label Content="Firstname:" Width="120" />
<TextBox x:Name="tbLastname" Height="25" Width="230" Margin="0,0,5,0"/>
</StackPanel>
</StackPanel>
<StackPanel Orientation="Horizontal" VerticalAlignment="Bottom" HorizontalAlignment="Right" Margin="10,0,10,10">
<Button Height="30" Width="70" Content="Close" Margin="10,0,0,0"/>
<Button Height="30" Width="70" Content="Save" Margin="10,0,0,0"/>
</StackPanel>
</Grid>
</Window>
The Model for user as class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Add_Edit_Delete.Model
{
class UserModel
{
public int Id { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
}
}
The ViewModel for user as class:
using Add_Edit_Delete.Model;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Add_Edit_Delete.ViewModel
{
class UserViewModel : INotifyPropertyChanged
{
public ObservableCollection<UserModel> listOfUser = new ObservableCollection<UserModel>();
UserModel newUser = new UserModel();
public UserModel NewUser
{
get => newUser;
set
{
if (newUser != value)
{
newUser = value;
this.RaisePropertyChanged(nameof(NewUser));
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
Database:
The whole project can be downloaded here:
I hope someone can show how to implement this in WPF with MVVM.
Thanks