Bind WPF to a collection, and bind WPF to a collection.

Source: Internet
Author: User

Bind WPF to a collection, and bind WPF to a collection.

What is a set view?

The collection view is a layer at the top of the bound source set. You can use sorting, filtering, and grouping queries to navigate and display the source set without changing the basic source set itself. The set view also maintains a pointer to the current item in the set. If the source set implements the INotifyCollectionChanged interface, the changes caused by the CollectionChanged event will be propagated to the view.

If you bind a widget to a set, WPF binds it to its default view. This default view is shared by all bindings directly to the same set. Therefore, a bound control or code changes to the default view (such as sorting or changes to the current item pointer) it is reflected in all other bindings of the same set.

Demonstrate shared set view

Sometimes the ItemsControl control is used to display the object set. When one of the items is selected, its details are displayed, as shown in:

<Button Content = "{Binding}"/> <Button Content = "{Binding Path =/}"/> <Button Content = "{Binding Path =/Description}"/>

The following code defines a display template for the data bound to ContentControl.

<DataTemplate x:Key="DetailTemplate">            <Border Width="300" Height="100" Margin="20" BorderBrush="Aqua" BorderThickness="1" Padding="8">                <Grid>                    <Grid.RowDefinitions>                        <RowDefinition/>                        <RowDefinition/>                        <RowDefinition/>                    </Grid.RowDefinitions>                    <Grid.ColumnDefinitions>                        <ColumnDefinition/>                        <ColumnDefinition/>                    </Grid.ColumnDefinitions>                    <TextBlock Grid.Row="0" Grid.Column="0" Text="First Name:"/>                    <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Path=FirstName}"/>                    <TextBlock Grid.Row="1" Grid.Column="0" Text="Last Name:"/>                    <TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Path=/LastName}"/>                    <TextBlock Grid.Row="2" Grid.Column="0" Text="Home Town:"/>                    <TextBlock Grid.Row="2" Grid.Column="1" Text="{Binding Path=/HomeTown}"/>                </Grid>            </Border></DataTemplate>

The following code demonstrates binding to a collection

<ListBox Width="200" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding Source={StaticResource MyFriends}}"/><ContentControl Content="{Binding Source={StaticResource MyFriends}}" ContentTemplate="{StaticResource DetailTemplate}"/>

Demonstrate how to create a set view

If you do not want to share a view, you can create a new view. The following Code demonstrates creating a static resource for a set view:

<Window.Resources><CollectionViewSource       Source="{Binding Source={x:Static Application.Current}, Path=AuctionItems}"         x:Key="listingDataView" /></Window.Resources>

CollectionViewSource is the proxy of the CollectionView class or the class derived from CollectionView. CollectionViewSource enables the XAML code to set common CollectionView attributes and pass these settings to the basic view. CollectionViewSource has a View that saves the actual View and a Source attribute that saves the Source set.

You can then bind the control to this view:

<ListBox Name="Master" Grid.Row="2" Grid.ColumnSpan="3" Margin="8"    ItemsSource="{Binding Source={StaticResource listingDataView}}"></ListBox>
Note:

CollectionViewSource is a proxy class, and the actual view saved in it is of the following type:

Source set type Set view type Description
IEnumerable Internal types based on CollectionView Unable to group items
IList ListCollectionView Fastest
IBindingList BindingListCollectionView  

The method to obtain the set view in code is as follows:

(ListCollectionView) view=(ListCollectionView)CollectionViewSource.GetDefaultView(rootElement.DataContext);

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.