MSDN Magazine - December 2007 - (Page 32) Figure 5 Object Binding you can bind to a CollectionViewSource instead of directly to the ObjectDataProvider. The CollectionViewSource then becomes the data source and serves as an intermediary that intercepts the data from the ObjectDataProvider; provides sorting, grouping, and filtering functionality; and then propagates it to the target. The CollectionViewSource shown next has its Source attribute set to the resource name of the ObjectDataProvider (persons). I then defined a sort order for the data by indicating the properties to sort by and their direction: xmlns attribute to the tag so that the svc shortcut is qualified and indicates the proper namespace: xmlns:svc=”clr-namespace:DataBindingWPF” The DataContext is used to bind all controls within a container control to a data source. This is very useful when you have several controls that all use the same binding source. The code could get repetitive if you indicated the binding source for every control. Instead, you can set the DataContext for the container of the controls to the binding source and simply omit the Source attribute from the contained controls. For example, here is a series of TextBlocks bound explicitly to the same binding source: Now that the data source is defined via the ObjectDataProvider, I want to bind items in a ListBox control to this data. I want to display two lines of text in each ListBoxItem. The first line will display the FullName property of the Person instance in bold and the second line will show the Title and City of the instance. In XAML, this is quite simple using DataTemplates, which allow you to define a data visualization strategy that can be reused. Figure 5 shows the completed XAML, which has a DataTemplate defined to display the Person information in the layout I designated. I set the DataType property of the DataTemplate to indicate that the DataTemplate will be referring to the Person class type. I do not specify the actual binding in the DataTemplate, as I will do this in the ListBox control. By omitting the Binding Source, the binding will be made to the current DataContext in scope. In Figure 5, I set the ListBox’s ItemsSource property to bind to the persons resource so that I could bind the data to the ListBox but not format it. The data is correctly displayed by setting the ItemTemplate property to the personLayout resource, which is the DataTemplate’s key name. The ultimate result is a screen that looks like Figure 6. Here are the same three TextBoxes bound to the DataContext, which refers back to the controls’ StackPanel container: If the container does not define a DataContext, then it will continue to look at the next outer nested container until it finds the current DataContext. Forging Ahead WPF data binding offers a great deal of flexibility and control over the type of data that can be bound to, and how it can be controlled and displayed. With so much power and so many choices, I’m sure you’ll want to get your hands dirty right away. Code download available at msdn.microsoft.com/msdnmag/code07.aspx. Send your questions and comments for John to mmdata@microsoft.com. John Papa is a Senior .NET Consultant with ASPSOFT (aspsoft.com) and a baseball fanatic who spends most of his summer nights rooting for the Yankees with his family and his faithful dog, Kadi. John, a C# MVP, has authored several books on ADO, XML, and SQL Server. He can often be found speaking at industry conferences such as VSLive or blogging at codebetter.com/blogs/john.papa. Sorting the Data Figure 6 Using a DataTemplate If you want to sort your data in a specific way, 32 msdnmagazine Data Points http://msdn.microsoft.com/msdnmag/code07.aspx http://aspsoft.com http://codebetter.com/blogs/john.papa http://codebetter.com/blogs/john.papa
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.