0

I'm using mvvmlight framework for wpf. I have main window where I put my frame (firstLoadedPage is just an example):

        <Frame Source="firstLoadedPage" />

I want to change my page to another one when I click button in

first loaded page

but I have no idea how can I achieve it. I've tried bind property like this to frame source:

public static string _myFrameSourcePath = firstLoadedPage
public string MyFrameSourcePath
{
    get { return _myFrameSourcePath; }
    set {
       _myFrameSourcePath = value;
       RaisePropertyChanged("MyFrameSourcePath");
    }
}

and then change value of _myFrameSourcePath when I click button in firstLoadedPage. It doesn't work so I tried change MyFrameSourcePath to static and then change value of MyFrameSourcePath instead of _myFrameSourcePath in Page. It also doesn't work.

Can somebody tell me how can I change my page placed in frame source in MainWindow when I click button inside this page to change current page to another?

d3st1ny
  • 75
  • 1
  • 13
  • When you write ``, are you missing a binding here? It should be something like ``. Also, be sure you're setting the public `MyFrameSourcePath` property which will also call the property change notification, not the private `_myFrameSourcePath`. On an related note, I'm not sure if it's the same thing but I wrote [this answer](https://stackoverflow.com/a/12216068/302677) a while back about navigating using MVVM... perhaps it will help you get going in the right direction? – Rachel May 31 '17 at 18:53
  • The point is that was just an example. I binded Souce from Frame before with property implemented in MainWindowViewModel. I want to override this property from MainWindowViewModel by clicking button in MyPage.xaml, this shoulda change my uri path in Source, but it doesnt work even for static variables. – d3st1ny Jun 01 '17 at 11:57

1 Answers1

1

You can do it this way. Below is a sample Xaml

<Grid>
    <StackPanel Orientation="Vertical">
        <TextBlock>Outside area of frame</TextBlock>
        <Frame Name="FrameWithinGrid"  Source="{Binding FrameSource}">
        </Frame>
        <Button x:Name="button1" Height="23" Margin="114,12,25,0" Command="{Binding GoToCommand}"   
            VerticalAlignment="Top" >Navigate to Msdn
        </Button>
    </StackPanel>


</Grid>

In Your ViewModel , some sample codes for example:

      private Uri _frameSource = new Uri("http://www.google.com", UriKind.Absolute);
      public Uri FrameSource
      {
         get { return _frameSource;}

         set
         {
            _frameSource = value;
            OnPropertyChanged("FrameSource");
         }
      }

      public ICommand GoToCommand
      {
         get
         {
            return new DelegateCommand(ExecuteGoTo);
         }
      }

      private void ExecuteGoTo()
      {
         FrameSource = new Uri("http://www.msdn.com", UriKind.Absolute);
      }

Thats all. Make sure your view model implements INotifyPropertyChanged. If you are using MVVM Light, change the DelegateCommand to RelayCommand

Luke
  • 58
  • 6
NoahV
  • 24
  • 3
  • I want to do something similar to your answer, NoahV, but in your solution I am changing page in window.xaml using button in this window.xaml. I want to change page by clicking button in page.xaml what should change property URI in my windowviewmodel. – d3st1ny Jun 01 '17 at 12:00
  • You should create a corresponding view model for page.xml, set the Command of the button to be for eg ExecuteCommand, in that view model for page.xml, declare ICommand as above – NoahV Jun 01 '17 at 15:27