title: Using Update button in WPF ListView to update Selected Item
body: <p>I have to make a WPF App to add, delete and update Items for Client Management purposes. I have so far been able to add and remove the items to the List using MVVM but I am unable to update the selected items. I require help in this regard.
  The code for the project is as follows:</p>
  <p><strong>Details Class.cs</strong></p>
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  
  
  
  namespace Assignment_2.Model
  {
      public class Details :INotifyPropertyChanged
      {
          private string name;
          public string Name
          { get { return name; } set { name = value; OnPropertyChanged(Name); } }
  
          private string company;
          public string Company 
          { get { return company; } set { company = value; OnPropertyChanged(Company); } }  
          
          private string region;
          public string Region 
          { get { return region; } set { region = value; OnPropertyChanged(Region); } }
  
  
          private string hra;
          public string HRA
          {
              get { return hra; }
              set
              {
                  hra = value;
                  OnPropertyChanged(HRA);
              }
          }
  
  
  
          private string basic;
          public string Basic
          { get { return basic; } 
              set { basic = value;
                  OnPropertyChanged(Basic);
              } 
          }
  
          private string pf;
          public string PF
          { get { return pf; } 
              set 
              { 
                  pf = value;
                  OnPropertyChanged(PF);
              } 
          }
  
          private string salary;
          public string Salary
          { get { return salary; } 
              set 
              { 
                  salary = value;
                  OnPropertyChanged(Salary);
              }
          }
  
         
  
          public event PropertyChangedEventHandler PropertyChanged;
          private void OnPropertyChanged(string det)
          {
              PropertyChangedEventHandler pc = PropertyChanged;
              if (pc != null)
                  pc(this, new PropertyChangedEventArgs(det));
              
          }
  
  
         
      }
  }
  </code></pre>
  <p><em><strong>DetailsViewModel.cs</strong></em></p>
  <pre><code>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 Assignment_2.Commands;
  using Assignment_2.Model;
  
  namespace Assignment_2.ViewModel
  {
      public class DetailsViewModel : INotifyPropertyChanged
      {
          
  
          private Details record;
          public Details Record
          {
              get { return record; }
              set { record = value; NotifyPropertyChanged(&quot;Record&quot;); }
          }
          private ObservableCollection&lt;Details&gt; records;
          public ObservableCollection&lt;Details&gt; Records
          {
              get { return records; }
              set { records = value; } //  NotifyPropertyChanged(&quot;Records&quot;);
          }
  
  
              
          private ICommand _SubmitCommand;
          public ICommand SubmitCommand
          {
              get
              {
                  if (_SubmitCommand == null)
                  {
                      _SubmitCommand = new RelayCommand(SubmitExecute, CanSubmitExecute, false);
                  }
                  return _SubmitCommand;
              }
          }
  
          public DetailsViewModel()
          {
              Record = new Details();
              Records = new ObservableCollection&lt;Details&gt;();
              
          }
           
  
          private void SubmitExecute(object parameter)
          {
              var TempRecord = new Details();
              //TempRecord = Record;
  
              TempRecord.Name = Record.Name;
              TempRecord.Company = Record.Company;
              TempRecord.Region = Record.Region;
              TempRecord.HRA = Record.HRA;
              TempRecord.Salary = Record.Salary;
              TempRecord.Basic = Record.Basic;
              TempRecord.PF = Record.PF;
  
              Records.Add(TempRecord);
          }
          
          private bool CanSubmitExecute(object parameter)
          {
              if (string.IsNullOrEmpty(Record.Name) || string.IsNullOrEmpty(Record.Company))
              {
                  return false;
              }
              else
              {
                  return true;
              }
          }
  
          private ICommand _SubmitCommand1;
          public ICommand SubmitCommand1
          {
              get
              {
                  if (_SubmitCommand1 == null)
                  {
                      _SubmitCommand1 = new RelayCommand(SubmitExecute1, CanSubmitExecute1, false);
                  }
                  return _SubmitCommand1;
              }
          }
  
          public int findIndex(Details myDetails)
          {
              int count = 0;
              for (int i = 0; i &lt; Records.Count; i++)
              {
                  count = 0;
                  Details myRecord = Records[i];
                  if (myRecord.Name == myDetails.Name)
                      count++;
                  else continue;
                  if (myRecord.Company == myDetails.Company)
                      count++;
                  else continue;
                  
                  if (myRecord.Region == myDetails.Region)
                      count++;
                  else continue;
                  if (myRecord.Salary == myDetails.Salary)
                      count++;
                  else continue;
                  if (myRecord.HRA == myDetails.HRA)
                      count++;
                  else continue;
                  if (myRecord.Basic == myDetails.Basic)
                      count++;
                  else continue;
                  if (myRecord.PF == myDetails.PF)
                      count++;
                  else continue;
  
                  if (count == 7)
                      return i;
              }
              return -1;
  
  
          }
  
          private void SubmitExecute1(object parameter)
          {
              Records.Remove((Details)parameter);
  
          }
          private bool CanSubmitExecute1(object parameter)
          {
              if (string.IsNullOrEmpty(Record.Name) || string.IsNullOrEmpty(Record.Company))
              {
                  return false;
              }
              else
              {
                  return true;
              }
          }
  
          private ICommand _SubmitCommand2;
          public ICommand SubmitCommand2
          {
              get
              {
                  if (_SubmitCommand2 == null)
                  {
                      _SubmitCommand2 = new RelayCommand(SubmitExecute2, CanSubmitExecute2, false);
                  }
                  return _SubmitCommand2;
              }
          }
  
  
          private void SubmitExecute2(object parameter)
          {
              
              
  
          }
          private bool CanSubmitExecute2(object parameter)
          {
              if (string.IsNullOrEmpty(Record.Name) || string.IsNullOrEmpty(Record.Company))
              {
                  return false;
              }
              else
              {
                  return true;
              }
          }
  
  
          public event PropertyChangedEventHandler PropertyChanged;
          protected void NotifyPropertyChanged(string de)
          {
              
              if (PropertyChanged != null)
                  PropertyChanged(this, new PropertyChangedEventArgs(de));
  
          }
  
         
  
  
      
       }
  }
  </code></pre>
  <p><em><strong>RelayCommand.cs</strong></em></p>
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows.Input;
  
  namespace Assignment_2.Commands
  {
      class RelayCommand : ICommand
      {
          Action&lt;object&gt; executeAction;
          Func&lt;object, bool&gt; canExecute;
          bool canExecuteCache;
  
  
          public RelayCommand(Action&lt;object&gt; executeAction, Func&lt;object, bool&gt; canExecute, bool canExecuteCache)
          {
              this.canExecute = canExecute;
              this.executeAction = executeAction;
              canExecuteCache = canExecuteCache;
          }
  
          public bool CanExecute(object parameter)
          {
              if (canExecute == null)
              {
                  return true;
  
              }
              else
              {
                  return canExecute(parameter);
              }
          }
  
          public event EventHandler CanExecuteChanged
          {
              add
              {
  
                  CommandManager.RequerySuggested += value;
  
              }
              remove
              {
  
                  CommandManager.RequerySuggested -= value;
  
              }
          }
  
          public void Execute(object parameter)
          {
              executeAction(parameter);
          }
      }
  }
  </code></pre>
  <p><em><strong>MainWindow.xaml</strong></em></p>
  <pre><code>&lt;Window x:Class=&quot;Assignment_2.MainWindow&quot;
          xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
          xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
          xmlns:d=&quot;http://schemas.microsoft.com/expression/blend/2008&quot;
          xmlns:mc=&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&quot;
          xmlns:vm=&quot;clr-namespace:Assignment_2.ViewModel&quot;  
  
          Title=&quot;MainWindow&quot; Height=&quot;401&quot; Width=&quot;472&quot;&gt;
  
      &lt;Window.Resources&gt;
          &lt;vm:DetailsViewModel x:Key=&quot;DetailsViewModel&quot;/&gt;
  
      &lt;/Window.Resources&gt;
      &lt;Grid DataContext=&quot;{Binding Source={StaticResource DetailsViewModel}}&quot; RenderTransformOrigin=&quot;0.5,0.5&quot; Margin=&quot;0,0,1,1&quot;&gt;
          &lt;Grid.RenderTransform&gt;
              &lt;TransformGroup&gt;
                  &lt;ScaleTransform/&gt;
                  &lt;SkewTransform/&gt;
                  &lt;RotateTransform Angle=&quot;-0.21&quot;/&gt;
                  &lt;TranslateTransform/&gt;
              &lt;/TransformGroup&gt;
          &lt;/Grid.RenderTransform&gt;
          &lt;Grid.RowDefinitions&gt;
              &lt;RowDefinition Height=&quot;Auto&quot;/&gt;
              &lt;RowDefinition Height=&quot;20.59&quot;/&gt;
              &lt;RowDefinition Height=&quot;22.906&quot;/&gt;
              &lt;RowDefinition Height=&quot;Auto&quot;/&gt;
              &lt;RowDefinition Height=&quot;29*&quot;/&gt;
              &lt;RowDefinition Height=&quot;293*&quot;/&gt;
          &lt;/Grid.RowDefinitions&gt;
          &lt;Grid.ColumnDefinitions&gt;
              &lt;ColumnDefinition Width=&quot;Auto&quot;/&gt;
              &lt;ColumnDefinition Width=&quot;*&quot;/&gt;
          &lt;/Grid.ColumnDefinitions&gt;
          &lt;TextBlock Grid.Row=&quot;0&quot; Text=&quot; Name&quot; HorizontalAlignment=&quot;Center&quot; Margin=&quot;0,0,0,4&quot; Grid.RowSpan=&quot;2&quot; Width=&quot;35&quot; /&gt;
          &lt;TextBox Grid.Column=&quot;1&quot; Width=&quot;100&quot; HorizontalAlignment=&quot;Left&quot; Text=&quot;{Binding Record.Name,UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}&quot; Margin=&quot;2,0,0,1&quot; Grid.RowSpan=&quot;2&quot;/&gt;
          &lt;TextBlock Text=&quot;Company&quot; HorizontalAlignment=&quot;Center&quot; Margin=&quot;0,2,0,3&quot; Width=&quot;51&quot; Grid.RowSpan=&quot;2&quot; Grid.Row=&quot;1&quot;/&gt;
          &lt;TextBox Grid.Column=&quot;1&quot; Width=&quot;100&quot; HorizontalAlignment=&quot;Left&quot; Text=&quot;{Binding Record.Company,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}&quot; Margin=&quot;2,2,0,1&quot; Grid.Row=&quot;1&quot;/&gt;
          &lt;TextBlock Grid.Row=&quot;2&quot; Text=&quot;Region&quot; HorizontalAlignment=&quot;Center&quot; Margin=&quot;0,1,0,4&quot; Width=&quot;37&quot;/&gt;
          &lt;TextBox Grid.Row=&quot;2&quot; Grid.Column=&quot;1&quot; Width=&quot;100&quot; HorizontalAlignment=&quot;Left&quot; Text=&quot;{Binding Record.Region,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}&quot; Margin=&quot;3,1,0,4&quot;/&gt;
          &lt;TextBlock Grid.Column=&quot;1&quot; Text=&quot;HRA&quot; HorizontalAlignment=&quot;Left&quot; Margin=&quot;131,-1,0,2&quot; Width=&quot;23&quot;/&gt;
          &lt;TextBox Grid.Column=&quot;1&quot; Width=&quot;83&quot; HorizontalAlignment=&quot;Left&quot; Text=&quot;{Binding Record.HRA,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}&quot; Margin=&quot;162,0,0,1&quot;/&gt;
          &lt;TextBlock Grid.Column=&quot;1&quot; Text=&quot;Basic&quot; HorizontalAlignment=&quot;Left&quot; Margin=&quot;129,2,0,13&quot; Width=&quot;26&quot; Grid.RowSpan=&quot;2&quot; Grid.Row=&quot;1&quot;/&gt;
          &lt;TextBox Grid.Column=&quot;1&quot; Width=&quot;81&quot; HorizontalAlignment=&quot;Left&quot; Text=&quot;{Binding Record.Basic,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}&quot; Margin=&quot;162,2,0,3&quot; Grid.Row=&quot;1&quot;/&gt;
          &lt;TextBlock Grid.Row=&quot;2&quot; Grid.Column=&quot;1&quot; Text=&quot;PF&quot; HorizontalAlignment=&quot;Left&quot; Margin=&quot;144,1,0,5&quot; Width=&quot;12&quot;/&gt;
          &lt;TextBox Grid.Row=&quot;2&quot; Grid.Column=&quot;1&quot; Width=&quot;100&quot; HorizontalAlignment=&quot;Left&quot; Text=&quot;{Binding Record.PF,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}&quot; Margin=&quot;162,2,0,1&quot;/&gt;
          &lt;TextBlock Grid.Column=&quot;1&quot; Text=&quot;Salary&quot; HorizontalAlignment=&quot;Left&quot; Margin=&quot;268,-1,0,1&quot; Width=&quot;36&quot;/&gt;
          &lt;TextBox Grid.Column=&quot;1&quot; Width=&quot;100&quot; HorizontalAlignment=&quot;Left&quot; Text=&quot;{Binding Record.Salary,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}&quot; Margin=&quot;304,0,0,21&quot; Grid.RowSpan=&quot;2&quot;/&gt;
          &lt;Button Content=&quot;Add&quot; Command=&quot;{Binding SubmitCommand }&quot; HorizontalAlignment=&quot;Left&quot; Grid.Row=&quot;4&quot; Margin=&quot;121,4,0,5&quot; Width=&quot;44&quot; Grid.Column=&quot;1&quot;/&gt;
  
  
  
          &lt;ListView x:Name=&quot;ListedView&quot; ItemsSource=&quot;{Binding Records}&quot; Width=&quot;Auto&quot; Grid.Row=&quot;5&quot; Margin=&quot;38,3,13,36&quot; Grid.ColumnSpan=&quot;2&quot;&gt;
  
              &lt;ListView.View &gt;
  
                  &lt;GridView &gt;
                      &lt;GridViewColumn Header=&quot;Name&quot; DisplayMemberBinding=&quot;{Binding Name}&quot; Width=&quot;Auto&quot; /&gt;
                      &lt;GridViewColumn  Header=&quot;Company&quot; DisplayMemberBinding=&quot;{Binding Company}&quot; Width=&quot;Auto&quot;/&gt;
                      &lt;GridViewColumn  Header=&quot;Region&quot; DisplayMemberBinding=&quot;{Binding Region}&quot; Width=&quot;Auto&quot; /&gt;
                      &lt;GridViewColumn  Header=&quot;HRA&quot; DisplayMemberBinding=&quot;{Binding HRA}&quot; Width=&quot;Auto&quot; /&gt;
                      &lt;GridViewColumn  Header=&quot;Basic&quot; DisplayMemberBinding=&quot;{Binding Basic}&quot; Width=&quot;Auto&quot; /&gt;
                      &lt;GridViewColumn  Header=&quot;PF&quot; DisplayMemberBinding=&quot;{Binding PF}&quot; Width=&quot;Auto&quot; /&gt;
                      &lt;GridViewColumn  Header=&quot;Salary&quot; DisplayMemberBinding=&quot;{Binding Salary}&quot; Width=&quot;Auto&quot; /&gt;
                      &lt;GridViewColumn&gt;
                          &lt;GridViewColumn.CellTemplate&gt;
                              &lt;DataTemplate&gt;
                                  &lt;Button Content=&quot;Remove&quot; Margin=&quot;1,36,50,150&quot; Command=&quot;{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ListView},Path=DataContext.SubmitCommand1}&quot;/&gt;
  
                                
                              &lt;/DataTemplate&gt;
                          &lt;/GridViewColumn.CellTemplate&gt;
                      &lt;/GridViewColumn&gt;
                  &lt;/GridView&gt;
              &lt;/ListView.View&gt;
          &lt;/ListView&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  <p><em><strong>Any help is appreciated in this regard. Thanks</strong></em></p>