Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 810999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:58:25+00:00 2026-05-15T00:58:25+00:00

I have written a MVVM prototype as a learning exercise and I am struggling

  • 0

I have written a MVVM prototype as a learning exercise and I am struggling to understand how I can communicate between views.Let me explain

I have a treeview on the left (leftSideView)

ListView on the right (rightSideView)

MainWindow (includes the 2 views mentioned above and a splitter)

What I have implemented does not work and I would like you if you can point out where I am going wrong or if there is a better way of doing it.
you can download the quick prototype from here

http://cid-9db5ae91a2948485.skydrive.live.com/self.aspx/.Public/WpfCommunicationBetweenViews.zip

For sure I am doing something wrong with the bindind.
Also it would be nice to learn how you do it. EG
ListBox on left (one view) ListView On the right (another view) how u communicate between the two.

Thanks a lot of any suggestions

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T00:58:26+00:00Added an answer on May 15, 2026 at 12:58 am

    I took a look at your code, made the modifications shown below, and it worked. I changed the right side view to just have a textblock to simplify it a bit.

    MainWindow.xaml.cs (Create a view model for both views to bind to)

    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    
        public static ProtoViewModel MainViewModel = new ProtoViewModel(Repository.GetContinents());
    }
    

    LeftSideView.xaml.cs (set the data context of this view to be the view model and update the selected city of the view model when changed)

    public partial class LeftSideView
    {
        public LeftSideView()
        {
            InitializeComponent();
    
            this.DataContext = MainWindow.MainViewModel;
        }
    
        /// <summary>
        /// Update the selected city of the view model
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnTreeSelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            (this.DataContext as ProtoViewModel).SelectedCity = e.NewValue as CityViewModel;
        }
    }
    

    RightSideView.xaml.cs (set the right side view to use the same view model)

    public partial class RightSideView
    {
        public RightSideView()
        {
            InitializeComponent();
    
            this.DataContext = MainWindow.MainViewModel;
        }
    }
    

    In the RightSideView.xaml, I just put the textbox that is shown below:

    <TextBlock Text="{Binding SelectedCity.Details.City.Name}"/>
    

    When a city on the left view is selected, it will changed the selected city on the view model, therefore, it will updated the selected city name on the right view.

    Here’s what the ProtoViewModel class looked like:

    public class ProtoViewModel : Core.ViewModelBase
    {
        public ProtoViewModel(IEnumerable<ContinentInfo> continents)
        {
            Continents =
                new ReadOnlyCollection<ContinentViewModel>(
                    (from continent in continents
                     select new ContinentViewModel(continent)).ToList());
        }
    
        public ViewModels.CityViewModel SelectedCity
        {
            get { return selectedCity; }
            set
            {
                if(selectedCity != value)
                {
                    selectedCity = value;
                    OnPropertyChanged("SelectedCity");
                }
            }
        }
        private ViewModels.CityViewModel selectedCity;
    
        public ReadOnlyCollection<ContinentViewModel> Continents
        {
            get { return continents; }
            set
            {
                if (continents != value)
                {
                    continents = value;
                    OnPropertyChanged("Continents");
                }
            }
        }
        private ReadOnlyCollection<ContinentViewModel> continents;
    }
    

    I would share the modified files with you, but I’m not sure how to do that 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a site in Prototype but want to switch to jQuery. Any
I am new to MVVM and have written a small app to test the
I have written an AIR Application that downloads videos and documents from a server.
I have written some code in my VB.NET application to send an HTML e-mail
I have written a ruby script which opens up dlink admin page in firefox
I have written an AppleScript which when supplied with a Windows network link, will
I have written a DLL that uses MS Word to spell check the content
I have written an assembly I don't want other people to be able to
I have written something that uses the following includes: #include <math.h> #include <time.h> #include
I have written a watir script that downloads files. One of the files it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.