First thing, You need to use a Collection type That will notify the UI of any changes. So this:
public const string MyListPropertyName = "ListBox";
private List<string> _listBox = null;
public List<string> ListBox
{
get
{
return _listBox = _listBox ?? new List<string>();
}
set
{
if (_listBox == value)
{
return;
}
RaisePropertyChanged(MyListPropertyName);
_listBox = value;
RaisePropertyChanged(MyListPropertyName);
}
}
can be changed to this:
public ObservableCollection<string> ListBox { get; set; }
= new ObservableCollection<string>();
Your XAML is missing so I can't see if the
SelectedItem
property is bound correctly.
I also can not check if your
DeleteCommand
is enabled correctly. The
RelayCommand
supports enabling and disabling the bound button. So you could wire it up like:
public RelayCommand DeleteCommand { get; private set; }
private void InitializeRelayCommands()
{
DeleteCommand = new RelayCommand
(() => DeleteSelectedItemListBox(),
() => isSelected);
}
private bool isSelected;
private string selectedItem;
public string SelectedItem
{
get { return selectedItem; }
set
{
Set(ref selectedItem, value);
isSelected = !string.IsNullOrEmpty(SelectedItem);
}
}
** The ViewModel needs to inherit the Galasoft
ViewModelBase
.
Now to delete the item, you only need to add the following where you want to remove the
SelectedItem
from the collection:
if (!string.IsNullOrEmpty(SelectedItem))
{
ListBox.Remove(SelectedItem);
SelectedItem = string.Empty;
}
Lastly, to prevent any confusion, don't call the collection
ListBox
but instead something meaningful like
ContactNames
and
SelectedItem
as
SelectedContact
. Now your code is self-describing with these two simple changes:
ContactNames.Remove(SelectedContact);
Also, you're using the incorrect namespace for the
RelayCommand
.Change:
using GalaSoft.MvvmLight.Command;
to:
using GalaSoft.MvvmLight.CommandWpf;
So your
MainWindowViewModel
now looks like:
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.CommandWpf;
using PhoneBook.View;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
namespace PhoneBook.ViewModel
{
class MainWindowViewModel : ViewModelBase
{
#region Constructor
public MainWindowViewModel()
{
InitializeRelayCommands();
ReadTextFile();
}
#endregion
#region Properties
public ObservableCollection<string> ContactNames { get; set; }
= new ObservableCollection<string>();
private bool isSelected;
private string selectedContact;
public string SelectedContact
{
get { return selectedContact; }
set
{
Set(ref selectedContact, value);
isSelected = !string.IsNullOrEmpty(selectedContact);
}
}
#endregion
#region Method
private void PopUpWindSelectedContact()
{
}
private void DeleteSelectedContactContactNames()
{
string FileName = (@"C: \Users\StanleyM\Desktop\PhoneBook\PhoneBook\bin\Debug\Personal.text");
StreamReader streamReader = new StreamReader(FileName);
string line = "";
int Counter = -1;
while ((line = streamReader.ReadLine()) != null)
{
foreach (var item in line)
{
if (item == SelectedContact)
{
Counter--;
ContactNames.Remove(line);
}
}
}
}
public void ReadTextFile()
{
string FileName = (@"C: \Users\StanleyM\Desktop\PhoneBook\PhoneBook\bin\Debug\Personal.text");
DataTable dt = new DataTable("Client");
StreamReader streamReader = new StreamReader(FileName);
string line = "";
int Counter = 0;
while ((line = streamReader.ReadLine()) != null)
{
Counter++;
ContactNames.Add(item: line);
}
}
private void PopUpWindow()
{
AddEditView PopUp = new AddEditView();
PopUp.ShowDialog();
}
#endregion
#region RelayCommand
public RelayCommand AddCommand { get; private set; }
public RelayCommand DeleteCommand { get; private set; }
private void InitializeRelayCommands()
{
AddCommand = new RelayCommand
(() => PopUpWindow());
DeleteCommand = new RelayCommand
(() => DeleteSelectedContactContactNames(),
() => isSelected);
}
#endregion
}
}