I'm trying to make a list of either customers or suppliers based on a radio button. I have a solution that works but I want to know if it is a correct solution to work with the MVVM-model. Next to that I want to know if I make correct use of the collecions. If there would be a more efficient way of coding for this part, I'd very happy to hear it.
I have one interface (customers and suppliers) which both implement an interface (IStakeholder). In my UI I have a two radio buttons and a combobox. Based on which radio button is used, another list should be displayed in the combobox. In my InputViewModel I use three list, one general for the combobox (CustomersSuppliers), one for customers (CustomerList) and one for Suppliers (SupplierList). The code is found below. To generate the INotifyProperty, I use Fody Weaver.
C# - ViewModel
public class InputViewModel : BaseViewModel
{
// Combobox list
public IEnumerable<Customer> CustomerList { get; set; }
/// <summary>
/// List with suppliers to choose from
/// </summary>
public IEnumerable<Supplier> SupplierList { get; set; }
/// <summary>
/// List with either customers or suppliers
/// Choice depends on radiobox in the View
/// </summary>
public ObservableCollection<IStakeholder> CustomersSuppliers { get; set; }
/// <summary>
/// Binds to the Customer Radio Button
/// </summary>
public bool ButtonCustomerIsChecked
{
get => _buttonCustomerIsChecked;
set
{
_buttonCustomerIsChecked = value;
if(value)
CustomersSuppliers = new ObservableCollection<IStakeholder>(CustomerList);
}
}
/// <summary>
/// Binds to the Supplier Radio Button
/// </summary>
public bool ButtonSupplierIsChecked
{
get => _buttonSupplierIsChecked;
set
{
_buttonSupplierIsChecked = value;
if(value)
CustomersSuppliers = new ObservableCollection<IStakeholder>(SupplierList);
}
}
C# - InputDesignModel (Dummy data)
public class InputDesignModel : InputViewModel
{
#region Singleton
public static InputDesignModel Instance = new InputDesignModel();
#endregion
#region Constructor
public InputDesignModel()
{
CustomersSuppliers = new ObservableCollection<IStakeholder>();
CustomerList = new ObservableCollection<Customer>
{
new Customer{Id=1, Name="Bart"},
new Customer{Id=1, Name="Nick"},
new Customer{Id=1, Name="Erwin"},
new Customer{Id=1, Name="Erwin"},
};
SupplierList = new ObservableCollection<Supplier>
{
new Supplier{Id=1, Name="Rita"},
new Supplier{Id=1, Name="Sascha"},
new Supplier{Id=1, Name="Didier"},
};
}
#endregion
}
WPF - UI
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<RadioButton Content="Klant"
Grid.Column="0"
Foreground="White"
Margin="10"
IsChecked="{Binding ButtonCustomerIsChecked}"
GroupName="CustomerSupplier"
Name="RadioButtonCustomer"
/>
<RadioButton Content="Leverancier"
Grid.Column="1"
Foreground="White"
Margin="10"
IsChecked="{Binding ButtonSupplierIsChecked}"
GroupName="CustomerSupplier"
Name="RadioButtonSupplier"
/>
</Grid>
<ComboBox ItemsSource="{Binding CustomersSuppliers}"
DisplayMemberPath="Name"
SelectedItem="{Binding Path=SelectedCustomerSupplier}"
Style="{StaticResource InputComboBox}"
/>
Model data
Istakeholder interface
public class Customer : IStakeholder
{
/// <summary>
/// The ID of the customer
/// </summary>
public int Id { get; set; }
/// <summary>
/// The name of the customer
/// </summary>
public string Name { get; set; }
/// <summary>
/// The phone number
/// </summary>
public string PhNr { get; set; }
/// <summary>
/// The VAT number
/// can be null
/// </summary>
public string VatNr { get; set; }
/// <summary>
/// The country where the customer is located
/// </summary>
public CountryCatEnum CountryCat { get; set; }
/// <summary>
/// A list of payments made by the customer
/// </summary>
public List<IPayments> Payments { get; set; }
}
Supplier class (customer is basicly the same)
public class Supplier : IStakeholder
{
/// <summary>
/// The name of the supplier
/// </summary>
/// <returns></returns>
public String Name { get; set; }
/// <summary>
/// The ID of the supplier
/// </summary>
public int Id { get; set; }
/// <summary>
/// The telephone number of the supplier
/// </summary>
public string PhNr { get; set; }
/// <summary>
/// The country
/// </summary>
public CountryCatEnum CountryCat { get; set; }
/// <summary>
/// The VAT number of the supplier
/// </summary>
public string VatNr { get; set; }
/// <summary>
/// The standard bank account number to pay to
/// </summary>
public string BankAcc { get; set; }
/// <summary>
/// List of payments made to this supplier
/// </summary>
public List<IPayments> Payments { get; set; }
}