0

I am learning WPF with M-V-VM. And I am using ICommand, RelayCommand. I have several Views, Models, and ViewModels. The MainWIndowView open upon on application start. The MainWindowView has a button that opens another WPF window called “FileListview” via MainWindowViewModel. The FileListView has a button “View Lookup”, which supposed to open another WPF window called “LookupView” via FileListViewModel. But I could not make this button to work unless I specify FileListView in App.xaml.cs instead of MainWIndowView. I could not understand why “View Lookup” button work if I make application to start from “FileListView” . I also don’t understand whether I need model for MainWindowView, and FileListView since I don’t have anything going except one view’s button is opening another view. On code behind file “App.xaml.cs” I have

public partial class App : Application
{
  protected override void OnStartup(StartupEventArgs e)
    {
      base.OnStartup(e);
      WPFProject. MainWIndowView window = new MainWIndowView ();
      MainWIndowViewModel  VM = new MainWIndowViewModel ();
      window.DataContext = VM;
      window.Show();
    }
 }

I would appreciate if somebody can point me to good article or sample code using WPF with M-V-VM that reflect my issue.

sll
  • 61,540
  • 22
  • 104
  • 156
Shai
  • 529
  • 7
  • 20
  • 36
  • 2
    1) Show code which open FileListview by a button click; 2) Show how you bind ViewModel to LookupView; 3) Show how you bind ViewModel to FileListview; – sll Aug 15 '11 at 20:50

2 Answers2

1

Here is my approach to use dialogs/child windows with mvvm and wpf. please note the comment from sllev and post all relevant code.

Community
  • 1
  • 1
blindmeis
  • 22,175
  • 7
  • 55
  • 74
0

After rethinking the issue, I was able to figure out the solution. The cause of the issue: I was not associating View with it’s ViewModel class. So I put the following code in code behind of FileListView.xaml.cs.

public partial class FileListView: Window
{
  private FileListViewModel  _ fileListViewModel = new FileListViewModel ();
  public FileListViewModel () 
  {
    InitializeComponent();
    base.DataContext = _fileListViewModel; 
  }
}

Thank you

Shai
  • 529
  • 7
  • 20
  • 36