Data Binding Demo

This sample creates a product listing application that allows users to enter items for sale.This sample demonstrates various data binding concepts.

 
 
 
 
 
4.5 Star
(18)
53,338 times
Add to favorites
2/24/2011
E-mail Twitter del.icio.us Digg Facebook
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Shapes;
using System.ComponentModel;

namespace DataBindingLab
{
    public partial class MainWindow : Window
    {
        CollectionViewSource listingDataView;

        public MainWindow()
        {
            InitializeComponent();
            listingDataView = (CollectionViewSource)(this.Resources["listingDataView"]);
        }
        

        private void OpenAddProductWindow(object sender, RoutedEventArgs e)
        {
            AddProductWindow addProductWindow = new AddProductWindow();
            addProductWindow.ShowDialog();
        }

        private void ShowOnlyBargainsFilter(object sender, FilterEventArgs e)
        {
            AuctionItem product = e.Item as AuctionItem;
            if (product != null)
            {
                // Filter out products with price 25 or above
                if (product.CurrentPrice < 25)
                {
                    e.Accepted = true;
                }
                else
                {
                    e.Accepted = false;
                }
            }
        }


        private void AddGrouping(object sender, RoutedEventArgs args)
        {
            // This groups the items in the view by the property "Category"
            PropertyGroupDescription groupDescription = new PropertyGroupDescription();
            groupDescription.PropertyName = "Category";
            listingDataView.GroupDescriptions.Add(groupDescription);
        }


        private void RemoveGrouping(object sender, RoutedEventArgs args)
        {
            listingDataView.GroupDescriptions.Clear();
        }

        private void AddSorting(object sender, RoutedEventArgs args)
        {
            // This sorts the items first by Category and within each Category,
            // by StartDate. Notice that because Category is an enumeration,
            // the order of the items is the same as in the enumeration declaration
            listingDataView.SortDescriptions.Add(
                new SortDescription("Category", ListSortDirection.Ascending));
            listingDataView.SortDescriptions.Add(
                new SortDescription("StartDate", ListSortDirection.Ascending));
        }

        private void RemoveSorting(object sender, RoutedEventArgs args)
        {
            listingDataView.SortDescriptions.Clear();
        }


        private void AddFiltering(object sender, RoutedEventArgs args)
        {
            listingDataView.Filter += new FilterEventHandler(ShowOnlyBargainsFilter);
        }


        private void RemoveFiltering(object sender, RoutedEventArgs args)
        {
            listingDataView.Filter -= new FilterEventHandler(ShowOnlyBargainsFilter);
        }
    }
}