You should first implement the INotifyPropertyChanged as follows:
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propertyname)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyname));
}
}
Define a property like this:
private string _contentToAdd;
public string ContentToAdd
{
get { return this._contentToAdd; }
set { this._contentToAdd = value; NotifyPropertyChanged("ContentToAdd"); }
}
In XAML, you can bind the textbox input to this property:
<textbox name="tb1" text="{Binding ContentToAdd}" />
And don't forget to set the data context of the view, otherwise, the binding won't work.
Last thing to do is to create a collection where you can add each entry you made in your text box and set the item source of your list box to the collection.
This should work...if you have any further questions, just ask.
EDIT:
Sure:
First, use this namespace:
using System.Collections.ObjectModel;
Then, define property for the collection:
private ObservableCollection<string> _yourList;
public ObservableCollection<string> YourList
{
get{return this._yourList;}
set{this._yourList = value; NotifyPropertyChanged;}
}
Don't forget to instantiate the collection in your constructor:
_yourList = new ObservableCollection<string>();
In your add command, add the string to this collection:
yourList.Add(ContentToAdd);
In XAML:
<ListBox ItemsSource ="{Binding YourList}"/>
EDIT v2:
Create new class:
class DelegateCommand : ICommand
{
private readonly Predicate<object> _canExecute;
private readonly Action<object> _execute;
public event EventHandler CanExecuteChanged;
public DelegateCommand(Action<object> execute)
{
_execute = execute;
}
public DelegateCommand(Action<object> execute, Predicate<object> canExecute)
{
_execute = execute;
_canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
if (_canExecute == null)
{
return true;
}
return _canExecute(parameter);
}
public void Execute(object parameter)
{
_execute(parameter);
}
public void RaiseCanExecuteChanged()
{
if (CanExecuteChanged != null)
{
CanExecuteChanged(this, EventArgs.Empty);
}
}
}
In your ViewModel:
public ICommand AddCommand{get;set;}
public void Add(object parameter)
{
YourList.Add(ContentToAdd);
}
Instantiate your command in your constructor:
AddCommand = new DelegateCommand(Add);
And finally in your XAML:
<Button Command="{Binding AddCommand}"/>
Try this please.