Pass the content of a ListBoxSelectedItem/SelectedValue to the service agent

Apr 13, 2013 at 10:15 PM
Ok, maybe the heading is a bit misleading. I'll try to elaborate it further.

Currently I have toll which saves the player characters of my RPG group. Those characters are displayed in a ListBox. This i got working with no problems. Every character is showing up fine so far.

The problem I have is the following.
If I double click on a character in the ListBox I want to populate a detail view on the right side of the window.
However to do that I somehow have to get the text of the selected item from the list box into the code behind.
The ListBox on the left and the detail view on the right are two separate views and view models.

My question is: How can I select a character from the ListBox on the left side and pass it to the detail view on the right side.

Greetings
Apr 14, 2013 at 4:16 AM
You can do this using the MessageBus within the toolkit. If you look in the documentation under code reference you can see some samples. I've done this exact process of passing data between view models and it is pretty straight forward. There may be some code samples. Post back if you need an example but I think you will be able to figure it out from the documentation.
Apr 14, 2013 at 2:05 PM
Ok, I guess I Understand what to do. I still got another problem tough.

How can I send the data from the ListBoxSelectedItem to the ViewModel?
Apr 14, 2013 at 4:40 PM
Hi,

The idea is to have two property in the viewmodel.
  1. ObservableCollection<Character> Characters
  2. Character SelectedCharacter
Now,
  • Bind the Characters as a ItemSource of the ListBox and design the ListBoxItem to display the character
  • Bind SelectedItem of the ListBox to the SelectedCharacter Property
  • Bind the Datacontext of the Detail section and bind the individual item to the property of the Character.
So, when you select an item, the detail will automatically display the details.

hope this helps.
Apr 14, 2013 at 6:42 PM
Edited Apr 14, 2013 at 7:11 PM
That works splendid. Thank you. However is there a possibility to only change the SelectedCharacter property if the selected item has been double-clicked?

//edit: I face another problem. I read the documentation on the MessageBus. So far this is straightforward. The only problem I now face is: How do I get the additional data from SendMessage<TOutgoing> on the receiving end?
This is my code so far:
public class CharacterListViewModel : ViewModelBase<CharacterListViewModel>
{
        // Fields

        // Some other fields here

        /// <summary>
        /// The selected character.
        /// </summary>
        private Character selectedCharacter;
        
        // Constructors

        // Constructors and events are here.

        /// <summary>
        /// Gets or sets the selected character.
        /// </summary>
        public Character SelectedCharacter
        {
            get
            {
                return this.selectedCharacter;
            }

            set
            {
                this.selectedCharacter = value;
                this.NotifyPropertyChanged(c => c.SelectedCharacter);
            }
        }

        // ...

        public void LoadCharacter()
        {
            this.SendMessage("Character", new NotificationEventArgs<Character>("char", this.SelectedCharacter));
        }
        
       // Additional non related code here
}
and the code on the receiving end.
public class SingleCharacterViewModel : ViewModelBase<SingleCharacterViewModel>
    {
        // Fields

        /// <summary>
        /// The service agent.
        /// </summary>
        private ICharacterServiceAgent serviceAgent;

        /// <summary>
        /// The character.
        /// </summary>
        private Character character;

        // Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="SingleCharacterViewModel"/> class.
        /// </summary>
        public SingleCharacterViewModel()
        {
            RegisterToReceiveMessages("Character", OnNavigationRequested);
        }

        private void OnNavigationRequested(object sender, NotificationEventArgs e)
        {
             // How to get the data in here???
        }
}
Apr 15, 2013 at 2:39 AM
In the receiving end, I would write a method with a signature:
        void CharacterManipulationMethod(object sender, NotificationEventArgs<Character> e)
        {
             var incomingCharacter = e.Data;

        }
You can then do anything with this incomingCharacter or just have this assigned to a property in this viewmodel of type "Character"

so your constructor will look like:
        public SingleCharacterViewModel()
        {
            RegisterToReceiveMessages("Char", CharacterManipulationMethod);
        }
Hope this helps.
Apr 15, 2013 at 3:20 PM
Ok, that seems straightforward. However, if I include your sample in my project I get the following error:

Expected a method with the 'void NotificationEventArgs<Character> e' signature
Apr 15, 2013 at 3:38 PM
Nevermind, I found it out myself. If I get back home I'll post a complete step-by-step guide how to do this. Maybe Tony is so nice and puts this into the programming reference so other beginners won't ahve the same problem (I looked up the discussions and found quite a number of topics about this).

BTW: Great Framework Tony. Thumbs up
Apr 16, 2013 at 2:35 AM
Hi,

Sorry my bad. We just need to make sure that the String token match. When Registering to receive, just make sure that the token is "Character" instead of "Char". It should work.

Anyway. glad to know that you worked it out.