通过 WPF 视图模型更改源来在框架中导航

发布于 2024-12-29 00:09:49 字数 1103 浏览 2 评论 0原文

我是 WPF 和 MVVM 新手。我的 WPF 应用程序的 mainWindowView 中有 Frame 。我已将框架的源绑定到视图模型的 SourcePage 属性:

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

在视图模型中,

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

最初我通过在视图模型构造函数中设置 sourcepage 值在该框架中加载了 selectTest 视图:

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

现在单击按钮,我需要执行一些数据库操作,然后我想在该框架中加载另一个视图。

嗨科林,感谢您的立即回复。但我也尝试过同样的方法,但它没有按预期工作。这是我的代码

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

I am new to WPF and MVVM. I have Frame in mainWindowView in my WPF application. I have bind the source of frame to SourcePage property of 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");
    }
  }
}

Initially I have loaded selectTest view in that frame by setting sourcepage value in viewmodel constructor:

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

Now on click of button, I need to perform some DB operations and after that I want to load another view in that frame.

Hi Colin, Thanks for ur immediate reply. But I have tried the same and it is not working 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";
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

来世叙缘 2025-01-05 00:09:49

要实现此目的:

  1. 将视图模型中的命令 (ICommand) 作为属性公开。它可以绑定到一个按钮,单击该按钮将执行您的命令。有关示例,请参阅 MSDN 上的命令概述
  2. 执行命令时,在视图模型中执行所需的数据库逻辑。
  3. 完成后,将 SourcePage 属性更改为下一页。视图将自动更新。

您可能还想向视图模型添加一个 IsBusy 布尔属性,该属性在处理数据库活动时为 true。您可以使用它来通过绑定禁用您的视图。

To achieve this:

  1. Expose a command (ICommand) from your view model as a property. This can be bound to a Button, which when clicked will execute your command. See the Commanding Overview on MSDN for examples.
  2. Perform the database logic you require in your view model when the command is executed
  3. When it has completed, change the SourcePage property to your next page. The view will update automatically.

You might also want to add an IsBusy boolean property to your view model which is true whilst your database activities are being processed. You can use this to disable you view via binding.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文