Beginner seriously in need of a simple example

Mar 11, 2012 at 6:10 PM

I've been trying to setup data-binding to a listbox for a few days now. I don't have any idea how to do it using simple mvvm. Can someone please for god sakes provide a small sample project that does listbox data binding using simple mvvm?

 

Mar 14, 2012 at 1:48 AM

The documentation has an example in getting started, I think.

Once set up properly, the binding is no different from any other binding example.

When learning to use the toolkit,  I followed the example for my first three or four views.  Look for articles by Josh Smith for MVVM

 

Here is a pretty simple example of databinding.

http://www.codeproject.com/Articles/26031/Simple-WPF-databinding-with-some-additional-WPF-go

 

Ken

Mar 14, 2012 at 4:05 AM

Ken,

 

I ended up finding some tutorials.I'm actually a beginner to C#.

 

What I am trying to achieve now is:

Currently, I have 3 views each with seperate viewmodels. I have an application bar located on the main view with a "Refresh" item. The main view has a pivot control setup that pivots to the 2 other views. When I press the refresh item in the application bar I want to call a function (both are different functions) located in the current view/view model in the pivot.

 

Do you think you could point in the right direction or give some sort of sample?

 

Thanks,

Julio

Mar 14, 2012 at 6:30 AM

Ok, I was able to get it working for one function using the MessageBus. Is there a way to know which view is loaded in the pivot?

Mar 14, 2012 at 7:34 PM

Unfortunately, I am running on Visual Studio Express and the advanced projects won't load, so the example of the message bus have been unavailable to me in a functional way.  I wish some of these concepts had simple one form examples.

below is some code that I use to open a View and get its datacontext. I am using an Event Handler to pass data back and forth from a dialog view. Not sure if that is what you are looking for.

public void AddItem() {
 // - create new item lookup view
 // - get its datacontext, 
// - register event request function
// - show the view as dialog

 

 

ItemMaintView imv = new ItemMaintView(); 
ItemMaintViewModel i =(ItemMaintViewModel)imv.DataContext;
i.ItemReturn += UpdateItemList;
imv.ShowDialog();
}

 

 

Mar 14, 2012 at 7:41 PM

 

I  have a "Popular" pivot item and a "new" pivot item.

In the mainviewmodel is this function:


       public ICommand RefreshListCommand // Binding to in the main view
        {
            get
            {
                return new DelegateCommand(RefreshList);
            }
        }

    public void RefreshList() // Sends  the message to the PopularForumsViewModel and NewForumsViewModel
        {
            SendMessage(MessageTokens.Refresh, new NotificationEventArgs("Popular"));
        }


In PopularForumViewModel is this:

  public PopularForumsViewModel(IPopularForumsServiceAgent serviceAgent)
        {
            this.serviceAgent = serviceAgent;
            RegisterToReceiveMessages(MessageTokens.Refresh, OnRefreshRequested);

        }

  void OnRefreshRequested(object sender, NotificationEventArgs e)
        {
          f(e.Message=="Popular")
            PopularDirectoryForum();
        }

In the NewForumsViewModel I have this:

   public NewForumsViewModel(INewForumsServiceAgent serviceAgent)
        {
            this.serviceAgent = serviceAgent;
            RegisterToReceiveMessages(MessageTokens.Refresh, OnRefreshRequested);

        }

        void OnRefreshRequested(object sender, NotificationEventArgs e)
        {
                 if(e.Message=="New")

                NewDirectoryForum();
        }


How can I have this portion ? How can send a conditional argument via NotificationEventArgs?

SendMessage(MessageTokens.Refresh, new NotificationEventArgs(somecondition ? "Popular":"New"));

Mar 14, 2012 at 8:41 PM

I achieved it by manually creating the viewmodel in the main view and add some code-behind to the main view.