Hi, try following demo. At first you must select typ of buffer to store items (lines). In demo I use ObservableCollection and txt file for saving items. In constructor I load the items (lines) and in destructor I save items (lines).
XAML:
<Window x:Class="Window010"
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:WpfApp1.WpfApp010"
mc:Ignorable="d"
Title="Window010" Height="450" Width="800">
<Window.DataContext>
<local:ViewModel/>
</Window.DataContext>
<StackPanel>
<TextBox Text="{Binding Text}" Margin="5"/>
<Button Content="Add Item" Command="{Binding Cmd}" Margin="5"/>
<ListBox ItemsSource="{Binding Col}" Margin="5"/>
</StackPanel>
</Window>
Code:
Imports System.Collections.ObjectModel
Imports System.ComponentModel
Imports System.IO
Imports System.Runtime.CompilerServices
Namespace WpfApp010
Public Class ViewModel
Implements IDisposable
Private filePath As String = "c:\temp\xTest.txt"
Public Sub New()
If File.Exists(filePath) Then
Col = New ObservableCollection(Of String)(File.ReadAllLines(filePath))
Else
Col = New ObservableCollection(Of String)
End If
End Sub
Public Property Text As String
Public Property Col As ObservableCollection(Of String)
Public ReadOnly Property Cmd As ICommand = New RelayCommand(Sub(state)
Col.Add(Text)
End Sub)
#Region "IDisposable Support"
Private disposedValue As Boolean
Protected Overridable Sub Dispose(disposing As Boolean)
If Not disposedValue Then
File.WriteAllLines(filePath, Col.ToArray)
End If
disposedValue = True
End Sub
Protected Overrides Sub Finalize()
Dispose(False)
MyBase.Finalize()
End Sub
Public Sub Dispose() Implements IDisposable.Dispose
Dispose(True)
End Sub
#End Region
End Class
Public Class RelayCommand
Implements ICommand
Private ReadOnly _execute As Action(Of Object)
Public Sub New(ByVal execute As Action(Of Object))
Me._execute = execute
End Sub
Public Function CanExecute(ByVal parameter As Object) As Boolean Implements ICommand.CanExecute
Return True
End Function
Public Custom Event CanExecuteChanged As EventHandler Implements ICommand.CanExecuteChanged
AddHandler(ByVal value As EventHandler)
AddHandler CommandManager.RequerySuggested, value
End AddHandler
RemoveHandler(ByVal value As EventHandler)
RemoveHandler CommandManager.RequerySuggested, value
End RemoveHandler
RaiseEvent(ByVal sender As Object, ByVal e As EventArgs)
End RaiseEvent
End Event
Public Sub Execute(ByVal parameter As Object) Implements ICommand.Execute
Me._execute(parameter)
End Sub
End Class
End Namespace
ViewModel in CSharp:
using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Input;
namespace WpfApp1
{
namespace WpfApp010
{
public class ViewModel : IDisposable
{
private string filePath = @"c:\temp\xTest.txt";
public ViewModel()
{
if (File.Exists(filePath)) Col = new ObservableCollection<String>(File.ReadAllLines(filePath));
else Col = new ObservableCollection<String>();
}
public string Text { get; set; }
public ObservableCollection<string> Col { get; set; }
public ICommand Cmd { get => new RelayCommand((state) => Col.Add(Text)); }
#region IDisposable Support
private bool disposedValue;
protected void Dispose(bool disposing)
{
if (!disposedValue) File.WriteAllLines(filePath, Col.ToArray());
disposedValue = true;
}
~ViewModel() => Dispose(false);
public void Dispose() => Dispose(true);
#endregion
}
public class RelayCommand : ICommand
{
private readonly Predicate<object> _canExecute;
private readonly Action<object> _action;
public RelayCommand(Action<object> action) : this(action, null) { }
public RelayCommand(Action<object> action, Predicate<object> canExecute) { _action = action; _canExecute = canExecute; }
public void Execute(object o) => _action(o);
public bool CanExecute(object o) => _canExecute == null ? true : _canExecute(o);
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
}
}
}