patterncsharpMinor
Managing Contact Collections
Viewed 0 times
contactmanagingcollections
Problem
I have a WPF/MVVM form that contains sections for managing Recipient Contacts, CC Contacts and BCC Contacts.
Each of the three sections has buttons/ICommands for 'Add', 'Clear', 'Previous' and 'Next' that move through the collection of objects.
I'm trying to work out how I can remove the repeated code.
I wire up the commands in my constructor:
Then I have the various command code:
```
public ICommand AddressBookRecipientCommand { get; set; }
public void AddressBookRecipientPressed()
{
this.Recipient = this.Details.Recipients.ReplaceAndReturn(this.Recipient, this.Outlook.GetContact());
}
public ICommand PrevRecipientCommand { get; set; }
public bool CanPressPrevRecipient()
{
return Details.Recipients.IndexOf(Recipient) != 0;
}
public void PrevRecipientPressed()
{
int i = Details.Recipients.IndexOf(Recipient);
Recipient = Details.Recipients[i - 1];
}
public ICommand Nex
Each of the three sections has buttons/ICommands for 'Add', 'Clear', 'Previous' and 'Next' that move through the collection of objects.
I'm trying to work out how I can remove the repeated code.
I wire up the commands in my constructor:
this.AddressBookRecipientCommand = new GenericCommand(AddressBookRecipientPressed);
this.ClearRecipientCommand = new GenericCommand(ClearRecipientPressed, CanClearRecipient);
this.NextRecipientCommand = new GenericCommand(NextRecipientPressed, CanPressNextRecipient);
this.PrevRecipientCommand = new GenericCommand(PrevRecipientPressed, CanPressPrevRecipient);
this.AddressBookCCCommand = new GenericCommand(AddressBookCCPressed);
this.ClearCCCommand = new GenericCommand(ClearCCPressed, CanClearCC);
this.NextCCCommand = new GenericCommand(NextCCPressed, CanPressNextCC);
this.PrevCCCommand = new GenericCommand(PrevCCPressed, CanPressPrevCC);
this.AddressBookBCCCommand = new GenericCommand(AddressBookBCCPressed);
this.ClearBCCCommand = new GenericCommand(ClearBCCPressed, CanClearBCC);
this.NextBCCCommand = new GenericCommand(NextBCCPressed, CanPressNextBCC);
this.PrevBCCCommand = new GenericCommand(PrevBCCPressed, CanPressPrevBCC);Then I have the various command code:
```
public ICommand AddressBookRecipientCommand { get; set; }
public void AddressBookRecipientPressed()
{
this.Recipient = this.Details.Recipients.ReplaceAndReturn(this.Recipient, this.Outlook.GetContact());
}
public ICommand PrevRecipientCommand { get; set; }
public bool CanPressPrevRecipient()
{
return Details.Recipients.IndexOf(Recipient) != 0;
}
public void PrevRecipientPressed()
{
int i = Details.Recipients.IndexOf(Recipient);
Recipient = Details.Recipients[i - 1];
}
public ICommand Nex
Solution
If I understand correctly what you're doing, I think what you need is a
Your main view would have 3 instances of that control, one for each field ("To", "CC", "BCC"), and each would have its own data context, some
Also you're not showing your entire class so it's hard to tell, but since you mention MVVM I believe a lot of this code belongs in a ViewModel class, not in the View's code-behind; the XAML markup uses DataBinding markup extensions to bind visual element properties to properties in the ViewModel - the Window's
The reason I mention this, is because ideally in MVVM the View should only be caring for presentation concerns, which clearly
UserControl that encapsulates some AddressBookCommand, ClearCommand, NextCommand and PreviousCommand commands.Your main view would have 3 instances of that control, one for each field ("To", "CC", "BCC"), and each would have its own data context, some
IList I'd guess.Also you're not showing your entire class so it's hard to tell, but since you mention MVVM I believe a lot of this code belongs in a ViewModel class, not in the View's code-behind; the XAML markup uses DataBinding markup extensions to bind visual element properties to properties in the ViewModel - the Window's
DataContext property is set to the instance of the ViewModel class.The reason I mention this, is because ideally in MVVM the View should only be caring for presentation concerns, which clearly
Details.Recipients.Add(Recipient); isn't.Context
StackExchange Code Review Q#44040, answer score: 3
Revisions (0)
No revisions yet.