Binding Data to Collections by Using Windows® Presentation Foundation

本文介绍如何使用ObservableCollection实现数据绑定的实时更新,并通过CollectionView进行数据的排序、过滤等操作而不改变原始数据。此外,还讲解了如何利用DataTemplate定义数据展示样式及应用触发器进一步定制数据展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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>

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值