WPF的数据绑定方法虽然很简单,但感觉实现的方法有很多种,目前没有一个标准,所以根据我已经看过的例子做了一个分类,分别是绑定到:对象、XML、控件和ADO.NET。

I. Binding to Object

1. Binding to a method using ObjectDataProvider

The data layer operations only in MyData.cs

sample:
http://winfx.members.winisp.net/files/samplemtapp.zip
http://blogs.msdn.com/karstenj/archive/2006/01/27/518499.aspx

Method GetMyDataObjects in Class Facroty returns object class MyDataObject which is inherit ObservableCollection<FOO>
FOO is another INotifyPropertyChanged interface Class, and FOO has property Description
(INotifyProperty means when the class object is changed it will send out notification)

Binding process:

1)Define namespace: xmlns:SampleMTApp="clr-namespace:SampleMTApp"

2)Define ObjectDataProvider: <ObjectDataProvider x:Key="FactoryDS" d:IsDataSource="True" ObjectType="{x:Type SampleMTApp:Factory}"/>

3)Binding data to listbox: <ListBox ItemsSource="{Binding GetMyDataObjects, Mode=Default, Source={StaticResource FactoryDS}}" />

4) DataTemplate
<DataTemplate x:Key="FOOTemplate1">
<StackPanel>
  <TextBlock Text="{Binding Description}"/>
</StackPanel>
</DataTemplate>


2. Binding DataContext in Window.DataContext (ObjectDataProvider not needed)

sample:
http://j832.com/work/namita/BasicDatabinding_MasterDetail.zip
http://blogs.msdn.com/namitag/archive/2005/09/21/472461.aspx

AuctionItem.cs: define Class AuctionItem : INotifyPropertyChanged
MyApp.xaml.cs: define Class.AuctionItems : ObservableCollection<AuctionItem> (create new items of AuctionItem)
Window1.xaml.cs: use [CollectionViewSource cv = root.DataContext as CollectionViewSource;] to action in button event handler

Binding Process:

1)Define DataContext in Window1.xaml:
  <Window.DataContext>
    <CollectionViewSource>
      <CollectionViewSource.Source>
        <local:AuctionItems/>

      </CollectionViewSource.Source>
    </CollectionViewSource>
  </Window.DataContext>

2) Binding Listbox:
<ListBox ItemsSource="{Binding Path=.}" IsSynchronizedWithCurrentItem="True">

3) Binding label: (the current item in listbox)
<Label Content="{Binding Path=/}" >

4) DataTemplate
<DataTemplate DataType="{x:Type local:AuctionItem}" >
...
<Image>
       <Image.Source>
                <Binding Path="Image"/>
       </Image.Source>
</Image>
...

More: this sample also shows how to use Converter and MultiBinding


3. Binding DataContext in controls to ObjectDataProvider

sample:
http://learnwpf.com/Data/Images/LearnWPF.DataTemplates.zip
http://learnwpf.com/Posts/Post.aspx?postId=386bae2f-5dc1-4b1d-8dd9-6ceed0428d88

MyApp.xaml:
<Application.Resources>
...
<ObjectDataProvider x:Key="Employees" ObjectType="{x:Type l:MSEmployeeCollection}"/>
</Application.Resources>

Window1.xaml:
<StackPanel Margin="10" DataContext="{StaticResource Employees}">
    ...
    <Button Content="{Binding Path=[0]}" />
    <ComboBox ItemsSource="{Binding}" SelectedIndex="0" />
</StackPanel>

1) DataContext another format : binding to StackPanel
2) Button bind to single item [0]
3) ComblBox bind to multi items, so no Path


II. Binding to XML

1. Use Source include .xml file to XmlDataProvider

sample code (copy & run in XamlPad):

<?xml version='1.0' encoding='utf-8'?>
<StackPanel xmlns="
http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Margin="10">
   <StackPanel.Resources>
      <XmlDataProvider x:Key="Blog" Source="
http://home.wangjianshuo.com/index.xml"/>
      <DataTemplate x:Key="TitleTemplate">
      <TextBlock Text="{Binding XPath=title}"/>
      </DataTemplate>
      </StackPanel.Resources>
   <Label Content="{Binding Source={StaticResource Blog}, XPath=/rss/channel/title}" FontSize="24" FontWeight="Bold" />
   <Label Content="{Binding Source={StaticResource Blog}, XPath=/rss/channel/description}" FontSize="18" />
   <DockPanel DataContext="{Binding Source={StaticResource Blog}, XPath=/rss/channel/item}" >
<ListBox DockPanel.Dock="Left" ItemsSource="{Binding}" ItemTemplate="{StaticResource TitleTemplate}" IsSynchronizedWithCurrentItem="True" />
<TextBox Name="Contents" Text="{Binding XPath=description}" TextWrapping="Wrap" Width="Auto" />
   </DockPanel>
</StackPanel>


2. Use x:XData define the xml structure inside current XmlDataProvider

sample code (copy & run in XamlPad):

<StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="
http://schemas.microsoft.com/winfx/2006/xaml">
  <StackPanel.Resources>
    <XmlDataProvider x:Key="FavoriteColors">
<x:XData>
  <Colors xmlns="">
   <Color>Blue</Color>
   <Color>Black</Color>
   <Color>Green</Color>
   <Color>Red</Color>
  </Colors>
</x:XData>
    </XmlDataProvider>
  </StackPanel.Resources>
  <TextBlock HorizontalAlignment="Center"
             FontWeight="Bold">
    XML Example
  </TextBlock>
  <ListBox Width="200" Height="300"
           ItemsSource="{Binding Source={StaticResource FavoriteColors},
           XPath=/Colors/Color}">
  </ListBox>
</StackPanel>


III. Binding to Control

1.
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
  <Canvas>
    <TextBox Name="theTextBox" Text="Hello" />
    <TextBlock Canvas.Top="25">
      <TextBlock.Text>
        <Binding ElementName="theTextBox" Path="Text" />
      </TextBlock.Text>
    </TextBlock>
  </Canvas>
</Window>

2.
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
  <Canvas>
    <TextBox Name="theTextBox" Text="Hello" />
    <TextBlock Canvas.Top="25"
               Text="{Binding ElementName=theTextBox, Path=Text}" />
  </Canvas>
</Window>


IX. Binding to ADO.NET

Similar to binding to object, create a dataset and return ds to DataContext.

Sample:
http://www.beacosta.com/Zips/18ThreeLevelMasterDetailADO.zip
http://www.beacosta.com/2006/03/how-do-i-bind-to-adonet.html