【问题标题】:How to Change View when OpenFileDialog is Closed?OpenFileDialog 关闭时如何更改视图?
【发布时间】:2014-01-30 22:14:49
【问题描述】:

这是我的第一个 MVVM 应用程序,我想知道在用户完成OpenFileDialog 后如何切换到另一个视图。

目前使用的改变视图技术是从here借来的。

换句话说,如何调用:

private void ExecuteGridViewCommand()
{
    CurrentViewModel = MainViewModel._gridViewModel;
}

由于 Dialog 不是 XAML 控件,因此我无法跟踪用户单击 Dialog 的“打开”按钮的时间。

    private static ViewModelBase _currentViewModel;
    public ViewModelBase CurrentViewModel
    {
        get { return _currentViewModel; }
        set
        {
            if (_currentViewModel != value)
            {
                _currentViewModel = value;
                OnPropertyChanged();
            }
        }
    }

【问题讨论】:

  • 你能展示你的CurrentViewModel属性吗?
  • @AnatoliiGabuza 你能详细说明一下吗?
  • 我的意思是展示你的属性 setter 和 getter 里面的内容。
  • 谢谢。我将属性代码添加到主帖中。我要做的是在对话框关闭后添加一个 bool 属性并将其绑定到 xaml,因此当它完成/为真时,我可以调用 ExcuteGridView 方法。不确定这种情况是否可行,但正在尝试。

标签: c# wpf mvvm openfiledialog


【解决方案1】:

之前有人发送了一个鼓舞人心的答案(已删除),尽管它没有开箱即用,可能是因为实例化了 ViewModelLocator() 的一个新实例。

原来是这样的:

private readonly MainViewModel _mainViewModel = (new ViewModelLocator().Main);
_mainViewModel.ExcuteGridView();

经过调整,现在我有了这个,它可以工作了:

ViewModelLocator.Main.ExcuteGridView();

为了让它工作,我必须在 ViewModelLocator 中将 Main 声明为静态:

 public static MainViewModel Main
    {
        get
        {
            return _main;
        }
    }

【讨论】:

  • 是的,我删除了我的答案,因为它没有为你提供解决方案。很高兴您找到了您所寻找的解决方案。
  • 谢谢。但是,您仍然可以在编辑后发布您的答案,我会接受它作为答案。
  • 如果这对你有用,你可以接受你自己的答案作为正确的答案。没问题。
猜你喜欢
  • 1970-01-01
  • 2011-04-05
  • 1970-01-01
  • 2021-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-24
  • 1970-01-01
相关资源
最近更新 更多