wpf 在视图模型之间传递数据的正确方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14412405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the correct way to pass data between view models?
提问by BeginnerCoder
I've just started with MVVM and I have been reading up on it and doing some examples. I've managed to create an application that will read from the database and then populate into a listbox. I am having difficulty in trying to link up the selected item into another view and then do a bit of processing in that views viewModel. Please can somebody explain to me the correct way to get the currently selected item from view1 listbox and then on view2 label just to output the selected item?
我刚刚开始使用 MVVM,我一直在阅读它并做了一些例子。我设法创建了一个应用程序,该应用程序将从数据库中读取然后填充到列表框中。我在尝试将所选项目链接到另一个视图,然后在该视图 viewModel 中进行一些处理时遇到困难。请有人向我解释从view1列表框中获取当前所选项目的正确方法,然后在view2标签上输出所选项目?
Here is my XAML:
这是我的 XAML:
<local:SecondView Margin="499,30,0,20">
<local:SecondView.DataContext>
<v:MainViewModel />
</local:SecondView.DataContext>
</local:SecondView>
<Button Height="22" HorizontalAlignment="Left" Margin="8,4,0,0" Name="button1" VerticalAlignment="Top" Width="48" Command="{Binding Path=GetDataCommand}">Button</Button>
<ListBox ItemsSource="{Binding Path=FileData}" SelectedItem="{Binding dr}" Height="330" HorizontalAlignment="Left" Margin="149,30,0,0" Name="listBox1" VerticalAlignment="Top" Width="250" DisplayMemberPath="DDFName" />
This piece of code is in my viewModel1:
这段代码在我的 viewModel1 中:
private DataRowView _dr;
public DataRowView dr
{
get{
return _dr;
}
set
{
_dr = value;
OnPropertyChanged("dr");}
}
I want to somehow get viewModel2 to get the new value of dr (which is the item selected in view1 listbox) and then on view2 I want to show some details
我想以某种方式让 viewModel2 获得 dr 的新值(这是在 view1 列表框中选择的项目),然后在 view2 上我想显示一些细节
Thanks in advance!
提前致谢!
采纳答案by fixagon
Your ViewModel2 class is depending on the Row --> One possibility to inject the dependency in ViewModel2 is to pass it by constructor.
您的 ViewModel2 类取决于 Row --> 在 ViewModel2 中注入依赖项的一种可能性是通过构造函数传递它。
public class ViewModel1
{
private DataRowView _dr;
public DataRowView dr
{
get
{
return _dr;
}
set
{
_dr = value;
OnPropertyChanged("dr");
this.DetailView = new ViewModel2(value); //On Change of the selected Row create a new viewModel which serves as detail view
}
}
private ViewModel2 _DetailView;
public ViewModel2 DetailView
{
get
{
return _DetailView;
}
set
{
if (_DetailView != value)
{
_DetailView = value;
RaisePropertyChanged(() => this.DetailView);
}
}
}
}
public class ViewModel2
{
public ViewModel2(DataRowView row)
{
this.Row = row;
}
private DataRowView _Row;
public DataRowView Row
{
get
{
return _Row;
}
set
{
if (_Row != value)
{
_Row = value;
RaisePropertyChanged(() => this.Row);
}
}
}
}
and in your XAML you can set the datacontext directly to the detail view:
在您的 XAML 中,您可以将数据上下文直接设置为详细信息视图:
<local:SecondView Margin="499,30,0,20" DataContext="{Binding DetailView, Mode=OneWay}" />
回答by LXG
you can use other solution, http://sharedprop.codeplex.com, read the source code is a way to share property between object, and works also if the second object is not active... check just for info
您可以使用其他解决方案,http://sharedprop.codeplex.com,阅读源代码是在对象之间共享属性的一种方式,如果第二个对象未处于活动状态,也可以使用...检查信息

