如何在ViewModels WPF,MVVM之间传递数据

时间:2018-09-18 09:25:49

标签: c# wpf mvvm

我已经在MainWindow中加载了图像,现在我试图在调用ChildWindow时显示输入图像,但无法将输入图像获取到ChildWindow。

如何将MainWindowViewModel中加载的输入图像传递给ChildWindowViewModel

我必须遵循什么方法来加载输入图像并将其传递给多个viewmodel

这是我的代码

MainWindowViewModel

public class MainWindowViewModel : ViewModelBase
{
    public ICommand OpenImg { get; set; }

    public MainWindowViewModel()
    {
        OpenImg = new RelayCommand(LoadImage);
    }

    private BitmapImage _image;
    public BitmapImage Image
    {
        get { return _image; }
        set
        {
            _image = value;
            RaisePropertyChanged("Image");
        }
    }

    private void LoadImage()
    {
        OpenFileDialog op = new OpenFileDialog();
        op.Title = "Select a Picture";
        op.Filter = "Images (*.BMP;*.JPG;*.GIF;*.PNG;*.TIFF)|*.BMP;*.JPG;*.GIF;*.PNG;*.TIFF|" +
            "All files (*.*)|*.*";
        op.Multiselect = true;
        if (op.ShowDialog() == true)
        {
            Image = new BitmapImage(new Uri(op.FileName));
        }
    }       

    private RelayCommand _childWin;
    public ICommand ChildWin
    {
        get
        {
            if (_childWin == null)
            {
                _childWin = new RelayCommand(DisplayChildWin);
            }
            return _childWin;
        }
    }

    private void DisplayChildWin()
    {
        ChildWindow childWindow = new ChildWindow();
        childWindow.Show();
    }
}

ChildWindowViewModel

public class ChildWindowViewModel : ViewModelBase
{
    public ChildWindowViewModel()
    {
        MainWindowViewModel = new MainWindowViewModel();

        Imge = MainWindowViewModel.Image;
    }

    private BitmapImage _imge;
    public BitmapImage Imge
    {
        get { return _imge; }
        set
        {
            _imge = value;
            RaisePropertyChanged("Imge");
        }
    }

    private MainWindowViewModel _mainWindowViewModel;
    public MainWindowViewModel MainWindowViewModel
    {
        get
        {
            return _mainWindowViewModel;
        }
        set
        {
            if (value != _mainWindowViewModel)
            {
                _mainWindowViewModel = value;
                RaisePropertyChanged("MainWindowViewModel");
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您可以通过多种折衷方法来处理此问题。为了使事情简单,并使用您提供的内容,我将按以下步骤进行。

我假设在您的ChildWindow类中,您将DataContext设置为new ChildWindowViewModel();,或者在XAML中创建了StaticResource。此解决方案需要您删除它。

仅需要BitmapImage的构造函数中的ChildWindowViewModel,并从中删除对MainWindowViewModel的了解。

public class ChildWindowViewModel : ViewModelBase
{
    public ChildWindowViewModel(BitmapImage image)
    {
        _imge = image;
    }

    private BitmapImage _imge;
    public BitmapImage Imge
    {
        get { return _imge; }
        set
        {
            _imge = value;
            RaisePropertyChanged("Imge");
        }
    }
}

现在,在您的MainWindowViewModel中,您可以按以下方式更新DisplayChildWin方法:

private void DisplayChildWin()
{
    var childWindow = new ChildWindow();
    var childWindowViewModel = new ChildWindowViewModel(Imge);
    childWindow.DataContext = childWindowViewModel;
    childWindow.Show();
}

我将尝试完全从您的ChildWindow类中删除MainWindowViewModel的创建。这是工厂最好处理的事情,可以将其注入MainWindowViewModel中。 Adaptive Code via C#是一本很棒的书,很好地介绍了Dependency Inversion,SOLID和一些简单的抽象。