0

I use Caliburn.Micro and I have 2 View and relative 2 ViewModel:

  • MainView (MainViewModel)
  • BView (BViewModel)

In BView i have a DataGrid and in BView a method to fill DataGrid. In MainView there is a Botton, I want you to click the button to open the window BView and call the methot to fill the DataGrid (the method name is:AllArticles).

So when I click the button (in MainWiew) will open BView with DataGrid filled.

The MainViewModel code is:

[Export(typeof(IShell))]
public class MainViewModel : Screen
{
    public string Path{ get; set; }

    public void Open()
    {
        OpenFileDialog fd = new OpenFileDialog();
        fd.Filter = "Text|*.txt|All|*.*";
        fd.FilterIndex = 1;

        fd.ShowDialog();

        Path= fd.FileName;
        NotifyOfPropertyChange("Path");
    }

}

The BViewModel code is:

public class BViewModel : Screen
{
    public List<Article> List { get; private set; }

    public void AllArticles()
    {
        Recover recover = new Recover();
        List = recover.Impor().Articles;
        NotifyOfPropertyChange("List");
    }    
}

What should I do?

4

1 回答 1

2

考虑使用 Caliburn 的 WindowManager。主视图模型中的代码可能如下所示:

    [Export(typeof(IShell))]
    public class MainViewModel : Screen
    {
        public string Path{ get; set; }

        [Import]
        IWindowManager WindowManager {get; set;}

        public void Open()
        {
            OpenFileDialog fd = new OpenFileDialog();
            fd.Filter = "Text|*.txt|All|*.*";
            fd.FilterIndex = 1;

            fd.ShowDialog();

            Path= fd.FileName;
            NotifyOfPropertyChange("Path");

            WindowManager.ShowWindow(new BViewModel(), null, null);
        }    
    }

另外,我注意到您的 MainViewModel 类上有 Export(IShell) 属性 - 这看起来不正确,因为 Screen 不是 IShell。

于 2013-03-25T03:07:20.173 回答