我有一个 View 模型,其中包含一个显示新窗口的命令(具有它自己的 View 模型)。新窗口的 View 模型具有各种属性,当我关闭新窗口时,我想将这些属性传递回调用它的第一个 View 模型。

我在这里打破了MVVM模式,因为用ShowDialog()显示新窗口似乎更快,更容易。这是我的代码示例:

MainViewModel:

//Note: The OnPropertyChanged and CanExecute functions are implemented

//method via command to show a new window
private void ShowSYOWindow(object parameter)
{
    SYOWindow syo = new SYOWindow();
    syo.ShowDialog();
}

新窗口的 View 模型:
class SYOWindowViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private DateTime _timeStart;
    public DateTime TimeStart
    {
        get { return _timeStart; }
        set { _timeStart = value; OnPropertyChanged("TimeStart"); }
    }

    private DateTime _timeStart2;
    public DateTime TimeStart2
    {
        get { return _timeStart2; }
        set { _timeStart2 = value; OnPropertyChanged("TimeStart2"); }
    }

    // Create the OnPropertyChanged method to raise the event
    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
}

这是SYOWindow的代码背后:
using Proj1.ViewModels; //viewmodel located in different folder in project
public partial class SYOWindow : Window
{
    public SYOWindowViewModel vm => (SYOWindowViewModel)DataContext; //error here

    public SYOWindow()
    {
        InitializeComponent();
    }

    private void OKBtn_Click(object sender, RoutedEventArgs e)
    {
        this.DialogResult = true;
    }

    private void CancelBtn_Click(object sender, RoutedEventArgs e)
    {
        this.DialogResult = false;
    }
}

因此,在从MainViewModel中使用“syo.ShowDialog()”打开新窗口,并在SYOWindow的SYOWindowViewModel中设置了一些属性后,我想将这些属性返回给MainViewModel。我不知道什么是“正确”,最干净的方法。我读过我可以强制转换DataContext,然后以这种方式获取值,但这似乎是错误的。但是,也许这是自从我使用showdialog打破MVVM以来的唯一方法。

最佳答案

ShowDialog()块,因此在ShowDialog()退出后,您可以让 parent 从 child 的viewmodel中获取东西:

private void ShowSYOWindow(object parameter)
{
    SYOWindow syo = new SYOWindow();
    syo.ShowDialog();

    var vm = (SYOWindowViewModel)syo.DataContext;

    this.Foo = vm.Foo;
}

如果您给SYOWindow一个ViewModel属性,可以稍微简化一下代码:
public partial class SYOWindow : Window
{
    //  ...stuff

    //  C#6 version -- but if you're on an earlier version of C#, you'll get
    //  "DataContext is a property but is used like a type" and a red squiggly.
    //public SYOWindowViewModel ViewModel => (SYOWindowViewModel)DataContext;

    //  C#<=5. This works fine in C#6 as well.
    public SYOWindowViewModel vm { get { return (SYOWindowViewModel)DataContext; } }

    //  ...more stuff
 }

然后,家长可以这样做:
private void ShowSYOWindow(object parameter)
{
    SYOWindow syo = new SYOWindow();

    syo.ShowDialog();

    this.Foo = syo.ViewModel.Foo;
}

如果SYOWindow使用its DialogResult property,则可以执行以下操作:
private void ShowSYOWindow(object parameter)
{
    SYOWindow syo = new SYOWindow();

    if (syo.ShowDialog().GetValueOrDefault())
    {
        this.Foo = syo.ViewModel.Foo;
    }
}

关于c# - 当另一个 View 模型的窗口关闭时,获取属性值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42070312/

10-17 01:07