【问题标题】:What is the correct way to pass data between view models?在视图模型之间传递数据的正确方法是什么?
【发布时间】:2013-01-19 08:08:42
【问题描述】:

我刚刚开始使用 MVVM,我一直在阅读它并做了一些示例。我设法创建了一个应用程序,该应用程序将从数据库中读取,然后填充到列表框中。我很难尝试将所选项目链接到另一个视图,然后在该视图 viewModel 中进行一些处理。请有人向我解释从 view1 列表框中获取当前选定项目然后在 view2 标签上仅输出选定项目的正确方法吗?

这是我的 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" />

这段代码在我的viewModel1中:

private DataRowView _dr;
    public DataRowView dr
    {
        get{
            return _dr;
        }

        set
        {
            _dr = value;
            OnPropertyChanged("dr");}
    }

我想以某种方式让 viewModel2 获得 dr 的新值(这是在 view1 列表框中选择的项目),然后在 view2 上我想显示一些细节

提前致谢!

【问题讨论】:

    标签: wpf mvvm viewmodel interaction


    【解决方案1】:

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

    在您的 XAML 中,您可以将 datacontext 直接设置为详细视图:

    <local:SecondView Margin="499,30,0,20" DataContext="{Binding DetailView, Mode=OneWay}" />
    

    【讨论】:

    • 感谢代码!我确实设法找到另一种方法来创建一个对象,即“datacontext”,然后设置我的 SecondView DataContext = datacontext。但你的方式更容易,现在我清楚如何在视图和视图模型之间传输数据。谢谢米尔!
    【解决方案2】:

    您可以使用其他解决方案,http://sharedprop.codeplex.com,阅读源代码是一种在对象之间共享属性的方法,如果第二个对象不活动也可以使用...检查信息

    【讨论】:

      猜你喜欢
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-04
      • 2015-12-07
      • 1970-01-01
      相关资源
      最近更新 更多