puti26
puti26

Reputation: 429

Open another view with caliburn micro

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

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?

Upvotes: 0

Views: 2736

Answers (1)

Volma
Volma

Reputation: 1345

Consider using WindowManager from Caliburn. Code in the main view model may look like this:

    [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);
        }    
    }

Also, I noticed you have Export(IShell) attribute on your MainViewModel class - which doesn't look right, because Screen is not IShell.

Upvotes: 2

Related Questions