First, you shouldn't do a query operation within the ViewModels constructor. You shouldn't do any "expensive" operation within the constructor of a class.
Second, ViewModels shouldn't be aware of other ViewModels. So you shouldn't instantiate a ViewModel within another ViewModel.
I would suggest a different approach. Instead of trying to pass an ID to the ViewModel directly, do it via an event aggregator. Event Aggregator is basically a messaging system to send messages in a decoupled way between ViewModels.
One ViewModel register to a certain event, the other ViewModel sends it's event without know if there is any subscriber to this event or who this subscriber may be. For example the PRISM IEventAggregator
.
You obtain an instance of the EventAgregator of your choice within your ViewModel (either by Dependency Injection or via Service Locator) and register your event in the constructor
public class LoanViewModel
{
public LoanViewModel()
{
IEventAggregator events = ... ; // get via ServiceLocator or via Constructor for DI
events.GetEvent<CustomerLoadedEvent>().Subscribe(OnCustomerLoaded);
}
private void OnCustomerLoaded(Customer customer)
{
int customerId = customer.ID;
// do your query now
}
}
public class OtherViewModel
{
IEventAggregator events;
public LoanViewModel()
{
this.events; = ... ; // get via ServiceLocator or via Constructor for DI
}
// Should be ICommand for WPF binding...assuming SelectedItem is from type Customer
public void Open()
{
if (SelectedItem != null)
{
events.GetEvent<CustomerLoadedEvent>().Publish(SelectedItem);
}
this.OnPropertyChanged("Items");
}
}
This way your ViewModels are completely decoupled from each other.
Of course PRISM/CompositeFramework may be a bit overkill for what you need. You can use other, simpler Event Aggregators or make your own one. Api may be different, the idea is the same