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>
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>
相关文章推荐
- Binding Data to Controls by Using Windows® Presentation Foundation
- Slow performance occurs when you copy data to a TCP server by using a Windows Sockets API program
- How to populate the datagrid on background thread with data binding by using Visual C#
- Building User Interfaces by Using Windows® Presentation Foundation
- Slow performance occurs when you copy data to a TCP server by using a Windows Sockets API program
- Adding 3-D Graphics and Multimedia by Using Windows® Presentation Foundation
- Customizing Appearance by Using Windows® Presentation Foundation
- How to sign a Driver by Windows …
- Generating Multivariate Normal Data by Using PROC IML & R
- A damn at han’s Windows phone book 笔记(6:page navigation & data binding)
- Using Intel® INDE 2015 to Develop OpenCL™ Applications
- How to use insert or retrieve data by using Core Data in iOS
- Writing Stereoscopic Software for StereoGraphics® Systems Using Microsoft Windows® OpenGL
- Using .NET Databinding to Display Ink from a Database
- How to get blob data using javascript XmlHttpRequest by sync
- How to send raw data to a printer by using Visual C# .NET
- [Project] Simulate HTTP Post Request to obtain data from Web Page by using Python Scrapy Framework
- .net垃圾回收学习【NET Best Practice No: 3:- Using performance counters to gather performance data】【翻译&&学习】
- Data Guard Physical Standby 11.2 RAC Primary to RAC Standby using a second network (Doc ID 1349977.1