Monday 1 November 2010

Silverlight ListBox Multi Select

For those who need to set the selected items (Multi Select) of the list box to a property,
you can use the following class:

     public static class ListBoxSelectedItems 
     {
         private static readonly DependencyProperty SelectedItemsBehaviorProperty =
             DependencyProperty.RegisterAttached(
                 "SelectedItemsBehavior" ,
                 typeof(SelectedItemsBehavior),
                 typeof(ListBox),
                 null);
 
         public static readonly DependencyProperty ItemsProperty = 
    DependencyProperty.RegisterAttached(
                 "Items" ,
                 typeof(IList),
                 typeof(ListBoxSelectedItems),
                 new PropertyMetadata(null, ItemsPropertyChanged));
 
         public static void SetItems(ListBox listBox, IList list) 
     {listBox.SetValue(ItemsProperty, list); }
         public static IList GetItems(ListBox listBox) 
     { return listBox.GetValue(ItemsProperty) as IList; }
 
         private static void ItemsPropertyChanged(DependencyObject d, 
    DependencyPropertyChangedEventArgs  e)
         {
             var  target = d as ListBox ;
             if  (target != null )
             {
                 GetOrCreateBehavior(target, e.NewValue as IList );
             }
         }
 
         private static SelectedItemsBehavior GetOrCreateBehavior(ListBox target, IList list)
         {
             var behavior = target.GetValue(SelectedItemsBehaviorProperty) 
        as SelectedItemsBehavior;
 
             if(behavior == null)
             {
                 behavior = new  SelectedItemsBehavior (target, list);
                 target.SetValue(SelectedItemsBehaviorProperty, behavior);
             }
 
             return behavior;
         }
     }
 
     public class SelectedItemsBehavior 
     {
         private readonly ListBox _listBox;
         private bool _isListChanging = false ;
         private bool _isSelectionChanging = false ;
         private IList _boundList;
 
         public SelectedItemsBehavior(ListBox listBox, IList boundList)
         {
             _boundList = boundList;
             _listBox = listBox;
             SetSelectedItems();
 
             // attach the selection changed event 
             _listBox.SelectionChanged += OnSelectionChanged;
 
             // attach the collection changed event 
             var collectionChanged = _boundList as INotifyCollectionChanged;
             if(collectionChanged != null )
             {
                 collectionChanged.CollectionChanged += BoundList_CollectionChanged;
             }
         }
 
         private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
         {
             // only fire this event when the list is not changing to prevent circular loop 
             if  (!_isListChanging)
             {
                 _isSelectionChanging = true;
 
                 _boundList.Clear();
 
                 foreach  (var item in _listBox.SelectedItems)
                 {
                     _boundList.Add(item);
                 }
 
                 _isSelectionChanging = false;
             }
         }
 
         private void BoundList_CollectionChanged(object sender, 
     System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
         {
             // only fire this event when the list is not changing to prevent circular loop 
             if  (!_isSelectionChanging)
             {
                 _isListChanging = true;
 
                 SetSelectedItems();
 
                 _isListChanging = false;
             }
         }
 
         private void SetSelectedItems()
         {
             if  (listBox.SelectionMode == SelectionMode .Single)
             {
                 if  (_boundList != null && _boundList.Count > 0)
                     _listBox.SelectedItem = _boundList[0];
             }
             else 
             {
                 _listBox.SelectedItems.Clear();
 
                 foreach(object item in _boundList)
                 {
                     // References in _boundList might not be the same as in _listBox.Items  
                     int  i = _listBox.Items.IndexOf(item);
                     if  (i >= 0)
                         _listBox.SelectedItems.Add(_listBox.Items[i]);
                 }
             }
         }
 
         private void ODataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
         {
             SetSelectedItems();
         }
     }
 


29 comments:

Anonymous said...

tramadol 100mg tramadol online buy - can you buy tramadol over the counter in spain

Anonymous said...

xanax online what type of drug is xanax - where to order xanax online no prescription

Anonymous said...

generic tramadol tramadol hcl amneal - tramadol addiction and pregnancy

Anonymous said...

generic xanax xanax for generalized anxiety disorder - xanax 2mg bars yellow

Anonymous said...

buy tramadol rx what is tramadol 50mg side effects - generic for tramadol

Anonymous said...

buy tramadol online tramadol 50 mg much take - tramadol dosage per pound

Anonymous said...

buy tramadol online can buy tramadol online legally - buy tramadol usa

Anonymous said...

alprazolam 0.5mg xanax withdrawal erowid - xanax side effects in cats

Anonymous said...

buy tramadol online tramadol dosage mims - where to buy tramadol online

Anonymous said...

buy carisoprodol cheap listaflex carisoprodol 350 mg para que sirve - buy cheap carisoprodol online

Anonymous said...

buy cheap tramadol tramadol tablets dogs dosage - buy tramadol er

Anonymous said...

discount cialis cialis price - buy cialis online us

Anonymous said...

cialis online generic cialis canadian - generic cialis dosage

Anonymous said...

buy cialis online generic cialis super active reviews - cialis online united states

Anonymous said...

xanax online pharmacy xanax side effects appetite - xanax side effects drugs.com

Anonymous said...

buy cialis online cialis online legale - ok buy cialis online

Anonymous said...

cialis online generic cialis mens health - buy cialis online germany

Anonymous said...

buy tramadol online buy tramadol online with visa - tramadol 50 mg rx list

Anonymous said...

http://blog.dawn.com/dblog/buy/#91875 buy tramadol online safe - tramadol 200mg er high

Anonymous said...

buy tramadol online tramadol withdrawal high blood pressure - tramadol 100mg safe

Anonymous said...

buy tramadol india buy tramadol personal check - tramadol hcl long term use

Anonymous said...

http://blog.dawn.com/dblog/buy/#fda tramadol sleep - buy tramadol cod overnight

Anonymous said...

buy tramadol buy tramadol with visa - buy tramadol florida

Anonymous said...

buy tramadol tramadol for dogs with food - buy tramadol online usa

Anonymous said...

buy tramadol tramadol medication overdose - generic brand for tramadol

Anonymous said...

order lorazepam ativan side effects breathing - ativan dosage for children

Anonymous said...

buy ativan lorazepam 1 mg tab - ativan recreational

Anonymous said...

xanax cost generic xanax 2mg pills - xanax 2mg a day

Anonymous said...

xanax 2mg do xanax 2mg look like - taking xanax for flight anxiety