views:

396

answers:

1

Hi all,

I'm investigating the Model View Presenter pattern. So far I like what I see, however pretty much all of the articles, webcasts, podcasts etc. I've seen, read or heard tend to deal with setting and retrieving simple types from textboxes and I'm struggling to understand how to deal with more complicated UI controls.

For example, imagine I have a CheckedListBox. In this CLB I want to display all available choices and the choices selected for a given instance (imagine a Friend class with a FavouriteIceCreamFlavours List). I can fill the list box easily, but how then would I set which are selected (say on a subsequent edit of this friend). Also, how would I then persist those changes back to the underlying Friend object?

Another candidate would be a TreeView. Suppose by right clicking a node in the TV I want the user to be able to delete that node - what's the best approach of communicating that action back to the Presenter?

Cheers,

Lenny.

(PS I'm developing in a C# 3.5/WinForms environment)

A: 

I'm only new to this MVP thing as well. But I'll have a go at what I would do. What I would do with the treeview is just handle the delete inside the view as it's just UI events, but if you some kind of database logic or something then you could do this.

I would have:

Presenter Interface:

Interface IPresenter
{
   bool DeleteItem(string itemName);
}

View Class:

class View : IView
{
   IPresenter presenter = new Presenter(this);

   void DeleteButtonClick(//SomeEventArgs)
   {
      bool vaild = this.presentor.DeleteItem(//Get the selected item);
      if (vaild)
      { //Delete the item from the tree view }
   } 
}

Presenter Class:

class Presenter : IPresenter
{
     public bool DeleteItem(string itemName)
     {
       // Check for valid delete.
       return true or false
     }
}

Hopefully that should work.

Nathan W