The first thing is: It is in 99% of all cases the best and possible to write you UI in XAML.
Second you should inform yourself, whats the WPF indicates and how to use the MVVM. If you don't you should stay by Windows Forms!
So now:
You ViewModel:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PB.Base;
namespace DynTabItems
{
public class MainWindowViewModel
{
public MainWindowViewModel()
{
AddItemCommand = new DelegateCommand(AddItem, CanAddItem);
RemoveItemCommand = new DelegateCommand(RemoveItem, CanRemoveItem);
}
public DelegateCommand AddItemCommand { private set; get; }
public DelegateCommand RemoveItemCommand { private set; get; }
ObservableCollection _yourBehaviorClass = new ObservableCollection();
MyModel _selectetItem;
public MyModel SelectetItem
{
get { return _selectetItem; }
set { _selectetItem = value; }
}
public ObservableCollection YourBehaviorClass
{
get { return _yourBehaviorClass; }
set { _yourBehaviorClass = value; }
}
private void AddItem(object obj)
{
YourBehaviorClass.Add(new MyModel());
}
private bool CanRemoveItem(object obj)
{
return SelectetItem != null;
}
private bool CanAddItem(object obj)
{
return true;
}
private void RemoveItem(object obj)
{
YourBehaviorClass.Remove(SelectetItem);
}
}
}
And now your XAML Code:
And last but not least MyModel:
using PB.Base;
using PB.Interfaces;
using PB.ValidationError;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DynTabItems
{
public class MyModel : ViewModelBaseEx
{
public MyModel()
{
YourHeader = "You header String";
ListOfExistingErrors = new ObservableCollection>();
ListOfExistingErrors.Add(new Error() { ErrorIndicator = "YourText", ErrorText = "Your Error Text", Predicate = s => string.IsNullOrEmpty(s.YourText) });
}
string _yourText;
string _yourHeader;
public string YourHeader
{
get { return _yourHeader; }
set
{
_yourHeader = value;
SendPropertyChanged(() => YourHeader);
}
}
public string YourText
{
get { return _yourText; }
set
{
_yourText = value;
SendPropertyChanged(() => YourText);
}
}
public override ObservableCollection> ListOfExistingErrors
{
get;
set;
}
}
}
If you need the example Project contact me.