通过 WPF 视图模型更改源来在框架中导航
我是 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要实现此目的:
ICommand
) 作为属性公开。它可以绑定到一个按钮,单击该按钮将执行您的命令。有关示例,请参阅 MSDN 上的命令概述。SourcePage
属性更改为下一页。视图将自动更新。您可能还想向视图模型添加一个 IsBusy 布尔属性,该属性在处理数据库活动时为 true。您可以使用它来通过绑定禁用您的视图。
To achieve this:
ICommand
) from your view model as a property. This can be bound to aButton
, which when clicked will execute your command. See the Commanding Overview on MSDN for examples.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.