2
votes

I have a WPF MVVM app that using PRISM. I have a toolbar UserControl which is bind to the ViewModel so it can invoke the commands on the ViewModels as shown below:

 <ToolBar Height="auto">
        <Button Content="New"/>
        <Button Content="Edit" Command="{Binding Path=EditCommand}" CommandParameter="{Binding SelectedItem}"  />
        <Button Content="Refresh"/>
        <Button Content="Delete"/>
        <Button Content="Close"/>
    </ToolBar>

The EditCommand invoke the following method in the ViewModel:

 private void Edit(UserViewModel userViewModel)
        {
            // load the edit page 



        }

In the Edit method I need to replace the current window with the edit window. How can I do that? I do not want to create or use UI elements in the Edit method because it goes against the MVVM model architecture.

1
Since you are already using PRISM, are you familiar with Interaction Requests? In MVVM you raise the request whenever you want a new dialog(usercontrol,window)Krishna
You can read more about InteractionRequests over here msdn.microsoft.com/en-us/library/…Krishna

1 Answers

-1
votes

Create a DataTemplate for the current window that you have and create triggers to change the Template of the window to the Edit template.

Sample trigger

<Style TargetType="Window">
  <Style.Triggers>
      <DataTrigger Binding="{Binding IsInEditMode}" Value="True">
       <Setter Property="Template" Value="{StaticResource EditModeTemplate}">
      </DataTrigger>
  </Style.Triggers>
</Style>

Sample data template, assuming you just want a TextBox

<DataTemplate x:Key="EditModeTemplate">
  <TextBox/>
</DataTemplate>

In your Edit method, you can just set the a boolean property to true or false whether to show an edit window or not.

public void Edit(UserViewModel viewModel)
{
  IsInEditMode = true;
}