Navigate in a frame by changing the source using the WPF view model

I am new to WPF and MVVM. I have a Frame in mainWindowView in my WPF application. I bind the frame source to the SourcePage property of the view model:

<Frame Name="frame" Content="Frame" Source="{Binding Path=SourcePage, Source={StaticResource WindowViewModel}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" /> 

In view model

 public string SourcePage { get { return _sourcePage; } set { if (value != null) { _sourcePage = value; OnPropertyChanged("SourcePage"); } } } 

I originally loaded selectTest view into this frame by setting the value of the original page in the viewmodel constructor:

 public MainWindowViewModel() { SourcePage ="Std.User/SelectTest.xaml"; } 

Now, by clicking the button, I need to perform some operations with the database, after which I want to load another view in this frame.

Hi Colin, thanks for the quick reply cheers. But I tried the same, and it does not work as expected. Here is my code

 public ICommand StartTestCommand { get { if (_startTest == null) { _startTest = new DelegateCommand(StartTest); } return _startTest; } } private void StartTest() { MainWindowViewModel mwvm = new MainWindowViewModel(); mwvm.SourcePage = "std.user/ChangePassword2.xaml"; } 
+4
source share
1 answer

To achieve this:

  • Derive the command ( ICommand ) from your view model as a property. This may be due to Button , which when clicked will execute your command. See Team Review on MSDN for examples.
  • Perform the required database logic in your view model when executing the command
  • When done, change the SourcePage property to the next page. The view will automatically update.

You can also add the IsBusy boolean property to your view model, which is true while your database actions are being processed. You can use this to turn off browsing through snapping.

0
source

Source: https://habr.com/ru/post/1392617/


All Articles