5
\$\begingroup\$

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:

        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 NextRecipientCommand { get; set; }
    public bool CanPressNextRecipient()
    {
        return !Recipient.IsNullOrEmpty() && Details.Recipients.IndexOf(Recipient) != MaxRecipients;
    }
    public void NextRecipientPressed()
    {
        if (Details.Recipients.IndexOf(Recipient) + 1 == Details.Recipients.Count)
        {
            Recipient = new Contact();
            Details.Recipients.Add(Recipient);
        }
        else
        {
            int i = Details.Recipients.IndexOf(Recipient);
            Recipient = Details.Recipients[i + 1];
        }
    }

    public ICommand ClearRecipientCommand { get; set; }
    public bool CanClearRecipient()
    {
        return !Recipient.IsNullOrEmpty();
    }
    public void ClearRecipientPressed()
    {
        int i = this.Details.Recipients.IndexOf(this.Recipient);
        if (i + 1 == this.Details.Recipients.Count)
            this.Recipient.Clear();
        else
        {
            this.Details.Recipients.Remove(this.Recipient);
            this.Recipient = this.Details.Recipients[i];
        }
    }

This code is repeated for CC and BCC.

\$\endgroup\$
3
\$\begingroup\$

If I understand correctly what you're doing, I think what you need is a 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<Contact> 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.

\$\endgroup\$
  • \$\begingroup\$ Thanks. I haven't used UserControls before. I'll take a look. Yes, the code above is from the ViewModel. \$\endgroup\$ – Matt Fitzmaurice Mar 13 '14 at 7:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.