2011-12-19 172 views
1

我有下面的代碼在我MainPage.xaml.cs中綁定視圖模型到MainPage.xaml中

public partial class MainPage : UserControl 
    { 
     private IsolatedStorageSettings userLogin = IsolatedStorageSettings.ApplicationSettings; 
     private UserDataContract currentUser; 
     private MainViewModel mainViewModel; 

     public MainPage() 
     { 
      InitializeComponent(); 
     } 

     // If an error occurs during navigation, show an error window 
     private void ContentFrameNavigationFailed(object sender, NavigationFailedEventArgs e) 
     { 
      e.Handled = true; 
      ChildWindow errorWin = new ErrorWindow(e.Uri); 
      errorWin.Show(); 
     } 

     private void Button_Click(object sender, RoutedEventArgs e) 
     { 
      HtmlPage.Window.Navigate(new Uri("#/Fields/Edit", UriKind.Relative)); 
     } 

    } 

現在,我想我的ViewModel到MainPage.xaml中綁定,我該怎麼辦呢?

  mainViewModel = new MainViewModel(); 
      this.LayoutRoot.DataContext = loginViewModel; 

並未真正發揮作用

回答

2
public MainPage() 
    { 
     InitializeComponent(); 
     mainViewModel = new MainViewModel(); 
     this.DataContext = mainViewModel ; 
    }