I have a user control ‘A’ that has another user control ‘B’ as its child. The usecase scenario being, when the user clicks a button on ‘A’, it should request ‘B’ to process some information (typically relating to the state of ‘B’) and get the result, which will then be used in some calculation in ‘A’.
Now, I am using MVVM pattern. The viewmodel of ‘A’ does not have any reference to either the UI of A or B. I am exactly looking for this: When a user cliks a button on the UI of ‘A’, I need to call a command in ‘B’, by passing a delegate in the viewmodel of ‘A’ as a parameter to that command. This way, when the command in ‘B’ processes information, it can then call the callback function passed from ‘A’.
Now, the delegate is parameter-less. Once the command in usercontrol ‘B’ processes some information, it updates its state. This state is a dependency property which will be binded to a property in the viewmodel of ‘A’.
Is there a way to achieve something like this?
I have a working solution currently. But I want to improvise the code and make it better. This is my current temporary workaround to this problem:
When the user clicks a button in ‘A’, I am having an Event Handler in ‘A’ (xaml.cs). In the event handler, as I have the reference of ‘B’, I am checking if the command in ‘B’ can be executed, and then call it. Then, I check if command (in viewmodel of ‘A’) can be executed, and then call it.
If my question was not descriptive enough, here is a code sample to support it:
XAML of ‘A’:
<bNameSpace:B x:Name="BObject" DepenPropertyInB="{Binding PropInA, Mode=OneWayToSource}"/>
....
<telerik:RadButton Content="Process" HorizontalAlignment="Right" Height="30" Width="80" Grid.Column="2" Click="ProcessButton_Click" />
XAML.cs of A:
private void ProcessButton_Click(object sender, RoutedEventArgs e)
{
if (BObject.StateDataRequestedCommand.CanExecute(""))
BObject.StateDataRequestedCommand.Execute("");
if(ViewModel.ProcessInfoRequestedCommand.CanExecute(""))
ViewModel.ProcessInfoRequestedCommand.Execute("");
}
ViewModel of A:
private void OnProcessInfoRequestedCommand(string anyValue)
{
// read the value of PropInA and do some processing
}
ViewModel of B:
private void OnStateDataRequestedCommand(string anyValue)
{
// Bases on B's state do some calc and update DepenPropertyInB
}
I’ll come back and add more detail to this later, but here’s basically what I think needs to happen.
The click should have a
DelegateCommandtype implementation, that contains a reference to the view models of both A and B. You need some kind of “conductor” class that is instantiating the view models, and which can pass the appropriate references into the command object.So, clearly the command belongs in the View Model for A, but, it needs a reference to B. So set up the command class with that in mind:
Now, but an instance of MyCommand into A’s View Model…
And bind to it as normal…
Now, the challenge is to instantiate this class within A’s View Model somehow. I normally use NInject to inject external dependencies into classes, but there are different ways to do it. Either way, it has to happen at a higher level than ViewModelA itself, since it doesn’t have a reference to ViewModelB. That could be in the App_Startup logic, or in IoC (eg, NInject) bindings, or in a View Model “Locator”, if that’s what you’re using.