Hi @Peter Fleischer (former MVP) , your code works great. I have been trying to implement new things lately. But with MVVM it is very time consuming for me. So now I want to program with code behind. But the simplest example (updating ListView) did not work. Is there something I don't understand correctly?
UserControl1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WPF_App.ucWindow;
using WPF_App.ViewModel;
namespace WPF_App.Usercontrol
{
/// <summary>
/// Interaktionslogik für UserControl1.xaml
/// </summary>
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
private void Add_Click(object sender, RoutedEventArgs e)
{
Window1 oAdd = new Window1();
oAdd.Owner = Application.Current.MainWindow;
oAdd.ShowDialog();
}
}
}
XAML
<UserControl x:Class="WPF_App.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:WPF_App.Usercontrol" xmlns:local1="clr-namespace:WPF_App.ViewModel"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800" Background="White">
<UserControl.DataContext>
<local1:UserViewModel/>
</UserControl.DataContext>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<ListView Grid.Column="0" ItemsSource="{Binding ListOfUser}">
<ListView.View>
<GridView>
<GridViewColumn Header="Firstname" x:Name="Firstname" Width="200">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Firstname}" TextWrapping="Wrap"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Lastname" x:Name="Lastname" Width="200">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Lastname}" 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" />
<Button Height="25" Width="50" Content="Delete" Margin="10"/>
</StackPanel>
</Grid>
</UserControl>
UserViewModel
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using WPF_App.Commands;
using WPF_App.Model;
using WPF_App.ucWindow;
namespace WPF_App.ViewModel
{
public class UserViewModel : INotifyPropertyChanged
{
public void SaveUsers()
{
this.ListOfUser.Add(NewUser.Copy());
RaisePropertyChanged(nameof(SaveUsers));
}
public UserModel NewUser { get; set; } = new UserModel();
public ObservableCollection<UserModel> ListOfUser { get; set; } = new ObservableCollection<UserModel>();
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string propertyName) =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
Window1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using WPF_App.ViewModel;
namespace WPF_App.ucWindow
{
/// <summary>
/// Interaktionslogik für Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void Save_Click(object sender, RoutedEventArgs e)
{
UserViewModel UserVM = new UserViewModel();
UserVM.SaveUsers();
}
private void Close_Click(object sender, RoutedEventArgs e)
{
}
}
}