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 eventvar 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 loopif (!_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 loopif (!_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.Itemsint i = _listBox.Items.IndexOf(item);if (i >= 0)_listBox.SelectedItems.Add(_listBox.Items[i]);}
}
}
private void ODataContextChanged(object sender, DependencyPropertyChangedEventArgs e){
SetSelectedItems();}
}
29 comments:
tramadol 100mg tramadol online buy - can you buy tramadol over the counter in spain
xanax online what type of drug is xanax - where to order xanax online no prescription
generic tramadol tramadol hcl amneal - tramadol addiction and pregnancy
generic xanax xanax for generalized anxiety disorder - xanax 2mg bars yellow
buy tramadol rx what is tramadol 50mg side effects - generic for tramadol
buy tramadol online tramadol 50 mg much take - tramadol dosage per pound
buy tramadol online can buy tramadol online legally - buy tramadol usa
alprazolam 0.5mg xanax withdrawal erowid - xanax side effects in cats
buy tramadol online tramadol dosage mims - where to buy tramadol online
buy carisoprodol cheap listaflex carisoprodol 350 mg para que sirve - buy cheap carisoprodol online
buy cheap tramadol tramadol tablets dogs dosage - buy tramadol er
discount cialis cialis price - buy cialis online us
cialis online generic cialis canadian - generic cialis dosage
buy cialis online generic cialis super active reviews - cialis online united states
xanax online pharmacy xanax side effects appetite - xanax side effects drugs.com
buy cialis online cialis online legale - ok buy cialis online
cialis online generic cialis mens health - buy cialis online germany
buy tramadol online buy tramadol online with visa - tramadol 50 mg rx list
http://blog.dawn.com/dblog/buy/#91875 buy tramadol online safe - tramadol 200mg er high
buy tramadol online tramadol withdrawal high blood pressure - tramadol 100mg safe
buy tramadol india buy tramadol personal check - tramadol hcl long term use
http://blog.dawn.com/dblog/buy/#fda tramadol sleep - buy tramadol cod overnight
buy tramadol buy tramadol with visa - buy tramadol florida
buy tramadol tramadol for dogs with food - buy tramadol online usa
buy tramadol tramadol medication overdose - generic brand for tramadol
order lorazepam ativan side effects breathing - ativan dosage for children
buy ativan lorazepam 1 mg tab - ativan recreational
xanax cost generic xanax 2mg pills - xanax 2mg a day
xanax 2mg do xanax 2mg look like - taking xanax for flight anxiety
Post a Comment