Click here to Skip to main content
15,896,269 members
Articles / Desktop Programming / WPF

Loading huge data in a Silverlight AutoCompleteBox

Rate me:
Please Sign up or sign in to vote.
4.78/5 (7 votes)
9 Aug 2011CPOL3 min read 55.8K   1.5K   18  
Creating a custom AutoCompleteBox which will filter large amounts of data quickly.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Collections;

namespace FastLoadingACB
{
    public partial class FastLoadingACB : UserControl//,INotifyPropertyChanged
    {
        #region Members
        private int counter = 0;
        public event SelectionChangedEventHandler FastLoadingACBSelectionChanged;
        #endregion

        #region Ctor
        public FastLoadingACB()
        {
            InitializeComponent();
        }
        #endregion

        #region Dependency Properties
        public double FastLoadingACBWidth
        {
            get { return (double)this.GetValue(FastLoadingACBWidthProperty); }
            set { this.SetValue(FastLoadingACBWidthProperty, value); }
        }
        public IEnumerable FastLoadingACBItemsSource
        {
            get { return (IEnumerable)this.GetValue(FastLoadingACBItemsSourceProperty); }
            set { this.SetValue(FastLoadingACBItemsSourceProperty, value); }
        }
        public int MaxItemsInDropDown
        {
            get { return (int)this.GetValue(MaxItemsInDropDownProperty); }
            set { this.SetValue(MaxItemsInDropDownProperty, value); }
        }

        public static readonly DependencyProperty FastLoadingACBWidthProperty =
         DependencyProperty.Register("FastLoadingACBWidth", typeof(double), typeof(FastLoadingACB), new PropertyMetadata(FastLoadingACBWidthChangedCallback));
       
        public static readonly DependencyProperty FastLoadingACBItemsSourceProperty =
         DependencyProperty.Register("FastLoadingACBItemsSource", typeof(IEnumerable), typeof(FastLoadingACB), new PropertyMetadata(FastLoadingACBItemsSourceChangedCallback));

        public static readonly DependencyProperty MaxItemsInDropDownProperty =
        DependencyProperty.Register("MaxItemsInDropDown", typeof(int), typeof(FastLoadingACB), new PropertyMetadata(10,null));

        private static void FastLoadingACBWidthChangedCallback(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            FastLoadingACB acb = obj as FastLoadingACB;

            if (null != acb)
                acb.acbFastLoading.Width = (double)e.NewValue;
        }

        private static void FastLoadingACBItemsSourceChangedCallback(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            FastLoadingACB acb = obj as FastLoadingACB;

            if (null != acb)
                acb.acbFastLoading.ItemsSource = (IEnumerable)e.NewValue;
        }

        #endregion
   
        #region Event Handlers

        private void acbFastLoading_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (null != FastLoadingACBSelectionChanged)
                FastLoadingACBSelectionChanged(sender, e);
        }

        private void acbFastLoading_Loaded(object sender, RoutedEventArgs e)
        {
            this.acbFastLoading.TextFilter = (search, value) =>
                {
                    if (value.Length > 0)
                    {
                        if ((this.counter < MaxItemsInDropDown) && (value.ToUpper().StartsWith(search.ToUpper()) || 
                                                                    value.ToUpper().Contains(search.ToUpper())))
                        {
                            this.counter++;
                            return true;
                        }
                        else
                            return false;
                    }
                    else
                        return false;
                };
        }

        private void acbFastLoading_TextChanged(object sender, RoutedEventArgs e)
        {
            this.counter = 0;
        }
        #endregion 
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) CA-CIB
Singapore Singapore
I am a .Net developer, currently working in Singapore worked wiith Societe Generale Global Solution Centre, Bangalore and was previously with Cognizant.I have more than 8 years of .Net experience in BFSI domain. I am an experienced developer in C#, VB.Net, Silverlight, WPF, WCF, LINQ, Entity Framework, SSIS, NHibernate, ASP.Net and SQL Server.

Comments and Discussions