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.
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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.