码迷,mamicode.com
首页 > Windows程序 > 详细

WPF学习日记——Window的DataContext绑定ViewModel

时间:2014-07-24 17:34:05      阅读:461      评论:0      收藏:0      [点我收藏+]

标签:blog   http   os   strong   io   width   art   re   

1、全局的ViewModel绑定:

    a)设定全局的ViewModel(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)绑定全局的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="600" Width="850" WindowStartupLocation="CenterScreen"
6 DataContext="{DynamicResource mainVM}">
7 </Window>

2、单独的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="600" Width="850" WindowStartupLocation="CenterScreen">
6     <Window.DataContext>
7         <local:MainViewModel/>
8     </Window.DataContext>
9 </Window>

WPF学习日记——Window的DataContext绑定ViewModel,布布扣,bubuko.com

WPF学习日记——Window的DataContext绑定ViewModel

标签:blog   http   os   strong   io   width   art   re   

原文地址:http://www.cnblogs.com/zuixieyang/p/3865678.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!