DataContext:  

1. 通常可以用页面绑定一个ViewModel的实例,通过设置页面的DataContext 属性。

var viewModel = new MainWindowViewModel(path);

window.DataContext = viewModel;

2. 也可以用一个Control绑定一个Object的实例,可以在code中设置或 通过Windows.Resource 来 添加: 

<Window.Resources><ObjectDataProvider x:Key="MyPhotos" ObjectType="{x:Type local:PhotoList}"/></Window.Resources>

<ListBox ItemsSource="{Binding Source={StaticResource MyPhotos}}"

3. 注意ItemsSource 绑定的是实现 IEnumerable的集合, 而DataContext 是绑定一个实例,一个object,可以是任何类型的。

 

ItemsControl:

<ItemsControl ItemsSource="{Binding Path=Commands}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Label Content="{Binding Path=DisplayName}"></Label>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>