Reputation: 9
In my WPF project, MVVM pattern. I can show different views according to the situation in the MainWindow. I could not find how to make the user confirmation request when switching views, and the view does not change if the user does not approve. Can you tell me your suggestions about this, if any, sample articles or projects? Thank you so much.enter image description here
<Window ....>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="200"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<StackPanel VerticalAlignment="Bottom" HorizontalAlignment="Center" Grid.Row="1"
Grid.Column="0" Grid.ColumnSpan="2" Orientation="Horizontal">
<Button Width="120" Margin="5" Command="{Binding UpdateViewCommand}"
CommandParameter="Home"/>
<Button Width="120" Margin="5" Command="{Binding UpdateViewCommand}"
CommandParameter="Portfolio"/>
</StackPanel>
<Frame Content="{Binding SelectedViewModel}"/>
</Grid>
</Window>
<Application ....">
<Application.Resources>
<ResourceDictionary>
<DataTemplate DataType="{x:Type viewModels:HomeViewModel}">
<views:Home/>
</DataTemplate>
<DataTemplate DataType="{x:Type viewModels:PortfolioViewModel}">
<views:Portfolio/>
</DataTemplate>
</ResourceDictionary>
</Application.Resources>
</Application>
<Page x:Class="pkyRaporCore.Views.Home"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:viewModels="clr-namespace:pkyRaporCore.ViewModels"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
Title="Home">
<Page.DataContext>
<viewModels:HomeViewModel/>
</Page.DataContext>
<Grid>
<ComboBox ItemsSource="{Binding Koleksiyon}" DisplayMemberPath="{Binding}" Width="200"/>
</Grid>
</Page>
<Page ....>
<Grid>
<StackPanel>
<TextBlock Text="Portfolio" Margin="5 5"/>
<ListBox Margin="5 5">
<ListBoxItem Content="Portfolio 1/6"/>
<ListBoxItem Content="Portfolio 2/6"/>
<ListBoxItem Content="Portfolio 3/6"/>
<ListBoxItem Content="Portfolio 4/6"/>
<ListBoxItem Content="Portfolio 5/6"/>
<ListBoxItem Content="Portfolio 6/6"/>
</ListBox>
</StackPanel>
</Grid>
</Page>
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
Window window = new MainWindow();
window.DataContext = new MainViewModel();
window.Show();
base.OnStartup(e);
}
}
public class MainViewModel : ViewModelBase, I
{
private readonly IRegionManager regionManager;
public DelegateCommand<string> NavigateCommand { get; set; }
private ViewModelBase selectedViewModel = new HomeViewModel();
public ViewModelBase SelectedViewModel
{
get { return selectedViewModel; }
set { selectedViewModel = value; OnPropertyChanged(); }
}
public ICommand UpdateViewCommand { get; set; }
public MainViewModel()
{
UpdateViewCommand = new UpdateViewCommand(this);
}
}
public class ViewModelBase: ObservableBase
{
}
public class ObservableBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
public class UpdateViewCommand : ICommand
{
private MainViewModel viewModel;
public UpdateViewCommand(MainViewModel viewModel)
{
this.viewModel = viewModel;
}
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
if (parameter.ToString() == "Home")
{
viewModel.SelectedViewModel = new HomeViewModel();
}
else if (parameter.ToString() == "Portfolio")
{
viewModel.SelectedViewModel = new PortfolioViewModel();
}
}
}
Upvotes: 0
Views: 126
Reputation: 1044
You can just ask user in MessageBox and do nothing if user doesn't accept changing view.
public class UpdateViewCommand : ICommand
{
private MainViewModel viewModel;
public UpdateViewCommand(MainViewModel viewModel)
{
this.viewModel = viewModel;
}
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
MessageBoxResult res = MessageBox.Show("Do you confirm changing view?", "", MessageBoxButton.YesNo);
if (res != MessageBoxResult.Yes)
return;
if (parameter.ToString() == "Home")
{
viewModel.SelectedViewModel = new HomeViewModel();
}
else if (parameter.ToString() == "Portfolio")
{
viewModel.SelectedViewModel = new PortfolioViewModel();
}
}
}
Upvotes: 1