我有一个简单的问题:在我的应用程序中,我有一个“mainview”/Mainwindow。用户可以从这里控制所有的事情,比如数据库查询、搜索等等。
但我需要在单击菜单按钮时打开一个属性窗口。
所以我将它绑定(bind)到我的SearchWindowCommand:
private ICommand searchwindowcommand;
public ICommand SearchWindowCommand
{
get
{
if (searchwindowcommand == null)
{
searchwindowcommand = new RelayCommand(p => ExcecuteSearchwindowcommand());
}
return searchwindowcommand;
}
}
public void ExcecuteSearchwindowcommand()
{
}
通常我会这样打开它:(我让应用程序正常工作,但我没有使用 mvvm,现在我必须重做应用程序并弄清楚 mvvm :) )
public void auswahl_click(object sender, RoutedEventArgs e)
{
Einstellungen suchwindow = new Einstellungen();
app_config_load(suchwindow);
suchwindow.Show();
}
我必须在 Executecommand 中写入什么才能显示另一个窗口?我应该为另一个 View 创建一个新的 View 模型吗? (我想是吧?)
编辑:我的启动应用程序代码:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
SetupBindings();
}
private void SetupBindings()
{
pViewModelList viewModel = new pViewModelList();
personlistview.DataContext = viewModel;
}
}
And should i create a new viewmodel for the other view? (i guess yes?)
是的。
What do i have to write into my Executecommand to show up the other window?
在这种情况下,我通常会在 ViewModel 中引发一个事件。 View 将一个处理程序附加到此事件,实际的 ShowDialog()
在此处理程序中执行。
这里有一些伪代码展示了这个想法:
// ViewModel
public class MainViewModel
{
public event EventHandler<EventArgs<UpdateViewModel>> UpdateRequested;
private void ExecuteUpdate()
{
if (this.UpdateRequested != null)
{
var childVm = new UpdateViewModel(/* parameters related to the object being updated */);
this.UpdateRequested(this, new EventArgs<UpdateViewModel>(childVm));
}
}
}
// View
public class MainView
{
public MainView()
{
var vm = new MainViewModel();
this.DataContext = vm;
vm.UpdateRequested += (sender, e) =>
{
var updateView = new UpdateView();
updateView.DataContext = e.Data; // Gets the instance of the viewModel here
updateView.ShowDialog();
};
}
}
我是一名优秀的程序员,十分优秀!