1, the Global ViewModel binding:
A) Set the global ViewModel (in App.xaml):
1 <application x:class= "Mytest.app" 2 xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:local= "Clr-namespace:mytest"5 startupuri= "MainWindow.xaml" >6 < Application.resources>7 <local:mainviewmodel x:key= "MAINVM"/>8 </ Application.resources>9 </Application>
b) Bind global ViewModel (MainWindow):
1 <window x:class= "Mytest.mainwindow"
2 xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3 xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"
4 xmlns:local= "Clr-namespace:mytest"
5 title= "MainWindow" height= "width=" 850 "windowstartuplocation=" Centerscreen "
6 datacontext= "{DynamicResource MAINVM}">
7 </Window>
2. Separate ViewModel bindings (MainWindow):
1 <window x:class= "Mytest.mainwindow" 2 xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:local= "Clr-namespace:mytest"5 title= "MainWindow" height= "" "Width=" 850 " windowstartuplocation= "Centerscreen" >6 <window.datacontext>7 <local:mainviewmodel/ >8 </Window.DataContext>
9 </Window>