0
votes

Note: this is a different question to the one here: Pass dependency property to child view

I am currently creating a usercontrol in wpf which consists of several 'screens' that the user will click through.

For each 'screen' I have created a view with it's own viewmodel (e.g. View1.xaml, View2.xaml). The main usercontrol can then access these views:

<UserControl.Resources>
    <local:ModuleBaseViewModel x:Key="ViewModelDataSource" />
</UserControl.Resources>

<Grid x:Name="LayoutRoot" DataContext="{Binding Source={StaticResource ViewModelDataSource}}">
    <local:View1 Visibility="Visible"/>
    <local:View2 Visibility="Hidden"/>
</Grid>

I would like to pass a button click from a view back down to the parent usercontrol so that I can hide that view and show the next one. Can someone explain to me how to do this?

Thanks!

1
You need to create an event in each view that is fired when the button is clicked, like OnButtonClicked, then your user control can register to the eventmusefan

1 Answers

0
votes

If you are using MVVM and WPF, chances are you are using an IoC container. Almost every IoC container contains a method of publishing an event and then subscribing to that event. In Prism, the code looks something like this:

EventAggregator.GetEvent<SomeEvent>().Publish(ParametersHere)

I would suggest you look at your options for publishing events and reacting to them. Then your parent view model can respond to the children's events.