从主窗口WPF MVVM C#中的另一个页面更改页面



我正在使用WPF的MVVMLIGHT框架。我有主窗口放置框架(firstLoadedPage只是一个示例(:

        <Frame Source="firstLoadedPage" />

单击

中的按钮时,我想将我的页面更改为另一个页面

首先加载页面

,但我不知道该如何实现。我尝试将这样的属性绑定到框架来源:

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

,然后在firstloadedPage中单击按钮时更改_myframesourcepath的值。它不起作用,因此我尝试将我的FramesourcePath更改为静态,然后更改MyFramesourcePath的值,而不是页面中的_MyFramesourcePath。它也行不通。

当我单击此页面内的按钮以将当前页面更改为另一个页面时,有人可以告诉我如何更改位于Mainwindow中的帧源中的页面?

您可以这样做。以下是样本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>

在您的ViewModel中,例如一些示例代码:

      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);
      }

就是全部。确保您的视图模型实现InotifyPropertychanged。如果您使用的是MVVM Light,请将授权程序更改为RelayCommand

最新更新