日期:2014-05-19  浏览次数:20997 次

请问WPF Windows的数据绑定里有没有对应Winform 2.0 BindingSource的东西?
或者说在WPF里进行‘下一条’,‘前一条’,‘最后一条’,‘第一条’等浏览时还是继续用winform的BindingSource?

另外就是控件绑定了对象列表后用什么取得当前项?winform里是BindingSource.Current,   WPF呢?

------解决方案--------------------
帮顶
------解决方案--------------------
[转贴]WPF DataContext and CurrentItem
Posted on 2007/2/13 14:23:29 by Jérôme Laban

DataBinding is one of the technologies I like the most.

Winforms did a fine job introducing the concept, but it was pretty easy to be stuck when trying to perform complex databinding. .NET 2.0 brought the concept of BindingSource, which eased the management of multiple "Current items " on a single form. You might have encountered this when creating multiple master/detail views on the same form. That was the case when you wanted to walk through a set of tables through their relations, say down to three levels.

WPF has a far more advanced DataBinding engine and introduces the concept of DataContext. There 's the notion of a hierarchy of DataContexts and a databound control uses the nearest DataContext available.

An example is better than a thousand words. Here 's a data source :

<XmlDataprovider x:key= "ops " xpath= "/data/level1 ">
<x:XData>
<data xmlns= " ">
<level1 name= "1 ">
<level2 name= "1-1 ">
<level3 name= "test "> Some Value </level3>
<level3> Yet an other value from level 3 </level3>
</level2>
<level2 name= "1-2 ">
<level3> Some other Value </level3>
<level3> Yet an other Value </level3>
</level2>
</level1>

<level1 name= "2 ">
<level2 name= "2-1 ">
<level3> Some Value </level3>
<level3> Yet an other value from level 3 </level3>
</level2>
<level2 name= "2-2 ">
<level3> Some other Value </level3>
<level3> Yet an other Value </level3>
</level2>
</level1>
</data>
</x:XData>
</xmldataprovider>
It 's a three level hierarchy, and I want to display this data, by recursively selecting each level in a ListBox to see its content.

Now, let 's bind this data to a list box, contained in a GroupBox to be a bit more readable :

<GroupBox Header= "Level 1 " DataContext= "{Binding Source={StaticResource ops}} ">
<ListBox ItemsSource= "{Binding} " DisplayMemberPath= "@name " IsSynchronizedWithCurrentItem= "True " />
</GroupBox>

This performs a binding to the attribute "name " of the level1 node list. The IsSynchronizedWithCurrentItem tells the listbox to set the CurrentItem of the current DataContext, which can be used to fill the next ListBox for the level.

Now, to add a new DataContext level, let 's add a new GroupBox, and a stack panel to have a nice layout :

<GroupBox Header= "Level 1 " DataContext= "{Binding Source={StaticResource ops}} " >
<StackPanel Orientation= "Horizontal ">
<ListBox ItemsSource= "{Binding} " DisplayMemberPath= "@name " IsSynchronizedWithCurrentItem= "True " />