Stanley Mabunda

Stanley Mabunda

  • 1.5k
  • 154
  • 809

Deleting Selected Item from a Listbox.

Sep 8 2017 9:13 AM
I am trying to deleted selected item from a listbox which gets its data from a textfile but I can not feed my Delete method with the right excution to accomplish the task. I I have highted my code in red
 
 
 
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using PhoneBook.View;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace PhoneBook.ViewModel
{
class MainWindowViewModel : ViewModelBase
{
#region Constructor
public MainWindowViewModel()
{
ReadTextFile();
}
#endregion
#region Properties
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);
}
}
private string _selectedItem = null;
public string SelectedItem
{
get
{
return _selectedItem;
}
set
{
if (_selectedItem != null)
{
return;
}
_selectedItem = value;
RaisePropertyChanged();
}
}
#endregion
#region Method
private void DeleteSelectedItemListBox()
{
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.ToString() == SelectedItem.ToString())
{
Counter--;
ListBox.Remove(line);
}
}
}
}
public void ReadTextFile()
{
string FileName = (@"C: \Users\StanleyM\Desktop\PhoneBook\PhoneBook\bin\Debug\Personal.text");
StreamReader streamReader = new StreamReader(FileName);
string line = "";
int Counter = 0;
while ((line = streamReader.ReadLine()) != null)
{
Counter++;
ListBox.Add(item: line);
}
}
private void PopUpWindow()
{
AddEditView PopUp = new AddEditView();
PopUp.ShowDialog();
}
The below is my view
 
 
#endregion
#region RelayCommand
private RelayCommand _addCommand = null;
public RelayCommand AddCommand
{
get
{
return _addCommand = _addCommand ?? new RelayCommand(() => PopUpWindow());
}
}
private RelayCommand _deleteCommand = null;
public RelayCommand DeleteCommand
{
get
{
return _deleteCommand = _deleteCommand ?? new RelayCommand(() => DeleteSelectedItemListBox());
}
}
#endregion
}
}
 
 
<Window x:Class="PhoneBook.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:PhoneBook"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/PhoneBook;component/Resource/Resource.xaml"></ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Border BorderThickness="5" BorderBrush="AliceBlue" Background="AntiqueWhite">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="25" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Label Grid.Row="0" Content="List of Clients" ></Label>
<Border Grid.Row="1" BorderThickness="5" Background="Black">
<ListBox SelectedItem="{Binding SelectedItem}"
ItemsSource="{Binding ListBox, UpdateSourceTrigger=PropertyChanged}"
Grid.Row="1">
</ListBox>
</Border>
<Button Grid.Row="1" Grid.Column="1" Width="75" Height="25" VerticalAlignment="Top" Margin="100 10 0 0" HorizontalAlignment="Left" Content="Add" Command="{Binding AddCommand}"></Button>
<Button Grid.Row="1" Grid.Column="1" Width="75" Height="25" VerticalAlignment="Top" Margin="100 60 0 0" HorizontalAlignment="Left" Content="Delete" Command="{Binding DeleteCommand}"></Button>
</Grid>
</Border>
</Window>
 

Answers (1)