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 971461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:58:11+00:00 2026-05-16T02:58:11+00:00

My generic question is as the title states, is it best to load data

  • 0

My generic question is as the title states, is it best to load data during ViewModel construction or afterward through some Loaded event handling?

I’m guessing the answer is after construction via some Loaded event handling, but I’m wondering how that is most cleanly coordinated between ViewModel and View?

Here’s more details about my situation and the particular problem I’m trying to solve:

I am using the MVVM Light framework as well as Unity for DI. I have some nested Views, each bound to a corresponding ViewModel. The ViewModels are bound to each View’s root control DataContext via the ViewModelLocator idea that Laurent Bugnion has put into MVVM Light. This allows for finding ViewModels via a static resource and for controlling the lifetime of ViewModels via a Dependency Injection framework, in this case Unity. It also allows for Expression Blend to see everything in regard to ViewModels and how to bind them.

So anyway, I’ve got a parent View that has a ComboBox databound to an ObservableCollection in its ViewModel. The ComboBox’s SelectedItem is also bound (two-way) to a property on the ViewModel. When the selection of the ComboBox changes, this is to trigger updates in other views and subviews. Currently I am accomplishing this via the Messaging system that is found in MVVM Light. This is all working great and as expected when you choose different items in the ComboBox.

However, the ViewModel is getting its data during construction time via a series of initializing method calls. This seems to only be a problem if I want to control what the initial SelectedItem of the ComboBox is. Using MVVM Light’s messaging system, I currently have it set up where the setter of the ViewModel’s SelectedItem property is the one broadcasting the update and the other interested ViewModels register for the message in their constructors. It appears I am currently trying to set the SelectedItem via the ViewModel at construction time, which hasn’t allowed sub-ViewModels to be constructed and register yet.

What would be the cleanest way to coordinate the data load and initial setting of SelectedItem within the ViewModel? I really want to stick with putting as little in the View’s code-behind as is reasonable. I think I just need a way for the ViewModel to know when stuff has Loaded and that it can then continue to load the data and finalize the setup phase.

Thanks in advance for your responses.

  • 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-16T02:58:11+00:00Added an answer on May 16, 2026 at 2:58 am

    For events you should use the EventToCommand in MVVM Light Toolkit. Using this you can bind any event of any ui element to relaycommand. Check out his article on EventToCommand at

    http://blog.galasoft.ch/archive/2009/11/05/mvvm-light-toolkit-v3-alpha-2-eventtocommand-behavior.aspx

    Download the sample and have a look. Its great. You won’t need any codebehind then. An example is as follows:

    <Page x:Class="cubic.cats.Wpf.Views.SplashScreenView"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
          xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
          xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
          xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras"
          mc:Ignorable="d" 
          d:DesignHeight="300" d:DesignWidth="300"
        Title="SplashScreenPage">
    
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="Loaded">
                <cmd:EventToCommand Command="{Binding LoadedCommand}" />
            </i:EventTrigger>        
        </i:Interaction.Triggers>
    
        <Grid>
            <Label Content="This is test page" />
        </Grid>
    </Page>
    

    and the view mode could be like this

     public class SplashScreenViewModel : ViewModelBase
        {
            public RelayCommand LoadedCommand
            {
                get;
                private set;
            }
    
            /// <summary>
            /// Initializes a new instance of the SplashScreenViewModel class.
            /// </summary>
            public SplashScreenViewModel()
            {
                LoadedCommand = new RelayCommand(() =>
                {
                    string a = "put a break point here to see that it gets called after the view as been loaded";
                });
            }
        }
    

    if you would like the view model to have the EventArgs, you can simple set PassEventArgsToCommand to true:

    <i:Interaction.Triggers>
                <i:EventTrigger EventName="Loaded">
                    <cmd:EventToCommand PassEventArgsToCommand="True" Command="{Binding LoadedCommand}" />
      </i:EventTrigger>        
    </i:Interaction.Triggers>
    

    and the view model will be like

    public class SplashScreenViewModel : ViewModelBase
    {
        public RelayCommand<MouseEventArgs> LoadedCommand
        {
            get;
            private set;
        }
    
        /// <summary>
        /// Initializes a new instance of the SplashScreenViewModel class.
        /// </summary>
        public SplashScreenViewModel()
        {
            LoadedCommand = new RelayCommand<MouseEventArgs>(e =>
            {
                var a = e.WhateverParameters....;
            });
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.