您的位置:首页 > 其它

Binding Data to Collections by Using Windows® Presentation Foundation

2010-05-05 09:43 423 查看
Binding to Collections of Objects

Observable collection classes implement the INotifyCollectionChanged interface. When you bind a control to an observable collection, the binding engine updates the control whenever the collection changes.

把Collection继承ObservableCollection 类,可以实现绑定控件和源数据保持同步。

设置:IsSynchronizedWithCurrentItem

="True"

<

ListBox IsSynchronizedWithCurrentItem="True" DisplayMemberPath="FirstName"

ItemsSource="{Binding Source={StaticResource listview}}"></ListBox>

Managing Data by Using Collection Views

Collection views enable you to sort, filter, and group collection data without having to requery the underlying data source. You can bind controls to collection views in the same way that you bind controls to collections.

CollectionView是在binding source collection之上的一层,允许对源数据进行导航/排序/过滤/分组等操作而不用修改数据。

通常在Resource中创建CollectionViewSource,然后在ItemSource中绑定该资源。

xmlns:com ="clr-namespace:System.ComponentModel;assembly=WindowsBase"

<

Window.Resources>

<

CollectionViewSource x:Key="listview" Source="{StaticResource list}" Filter="CollectionViewSource_Filter">

<CollectionViewSource.SortDescriptions>

<com:SortDescription PropertyName="FirstName" Direction="Descending"> </com:SortDescription>

</CollectionViewSource.SortDescriptions>

</CollectionViewSource>

</

Window.Resources>

Creating Master-Detail User Interfaces

You can create a master-detail UI by data-binding two controls to the same data source. The detail control displays the detailed information for the object that is displayed in the master control.

利用itemControl和ContentControl实现。

Presenting Collection Data by Using Data Templates

You can use data templates to define how data is presented within your controls. You can also apply triggers to data templates to further customize how your data is displayed.

A data template is a tree of XAML elements that you can use to define how your controls visually present data.

推荐的做法把DateTemplate定义在Resouce里面,然后把ListBox的ItemTemplate属性绑定该Resource。

<Window.Resources>

<

DataTemplate x:Key="myDateTemplate">

<WrapPanel>

<TextBlock Text="{Binding Path=FirstName}" />
<

TextBlock Text=":"/>

<TextBlock Text="{Binding Path=LastName}" />

</WrapPanel>

<DataTemplate.Triggers>

<DataTrigger Binding="{Binding Path=FirstName}">

</DataTrigger>

</DataTemplate.Triggers>

</DataTemplate>

</Window.Resources>

======================

<

ListBox ItemsSource="{Binding}" IsSynchronizedWithCurrentItem="True" ItemTemplate="{StaticResource myDateTemplate}">

</ListBox>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐