2013-10-14 79 views
1

我無法清除ListBox.SelectedItems集合。請建議我做錯了什麼。 我以不同的方式清除集合,但它會留下以前的集合。無法清除ListBox.SelectedItems集合wpf

我清楚的集合,使得:

chListBox.SelectedItems.Clear(); 

chListBox.UnselectAll(); 

chListBox.SetSelectedItems(new ArrayList()); 

我的代碼:

public class CheckListBox : ListBox 
    { 
     public CheckListBox() 
     { 
      this.SelectionChanged += CheckListBox_SelectionChanged; 
      this.Resources = Application.LoadComponent(new Uri("/TASWpfControls;component/Resources/CheckListBoxResources.xaml", UriKind.RelativeOrAbsolute)) as ResourceDictionary; 
      this.ItemContainerStyle = this.Resources["CheckListBoxItem"] as Style; 
      this.AddHandler(ButtonBase.ClickEvent, new RoutedEventHandler(ClickEventHandler)); 
     } 

     private void ClickEventHandler(object sender, RoutedEventArgs routedEventArgs) 
     { 
      RoutedEventArgs eventArgs = new RoutedEventArgs(ItemSelectedEvent); 
      this.RaiseEvent(eventArgs); 
     } 

     public string PropertyName { get; set; } 

     public string PropertyCompare { get; set; } 

     public static readonly DependencyProperty SelectedSourceProperty = 
      DependencyProperty.Register("SelectedSource", typeof(IList), typeof(CheckListBox), new PropertyMetadata(SelectedSourceChanged)); 

     public IList SelectedSource 
     { 
      get { return (IList)GetValue(SelectedSourceProperty); } 
      set { SetValue(SelectedSourceProperty, value); } 
     } 

     public static RoutedEvent ItemSelectedEvent = 
      EventManager.RegisterRoutedEvent("ItemSelected", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(CheckListBox)); 

     public event RoutedEventHandler ItemSelected 
     { 
      add { AddHandler(ItemSelectedEvent, value); } 
      remove { RemoveHandler(ItemSelectedEvent, value); } 
     } 

     protected static void SelectedSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
     { 
      CheckListBox chListBox = d as CheckListBox; 
      if (chListBox != null) 
       chListBox.SortListBox(chListBox, e.NewValue); 
     } 

     protected virtual void SortListBox(CheckListBox chListBox, object newValue) 
     { 
      chListBox.SelectedSource = newValue as IList; 
      IList selectedItems = chListBox.SelectedSource; 
      chListBox.SelectedItems.Clear(); 
      if (selectedItems != null && selectedItems.Count > 0) 
      { 
       foreach (object selectedItem in selectedItems) 
       { 
        foreach (object item in chListBox.Items) 
        { 
         if (eIReflector.GetValue(item, chListBox.PropertyName).Equals(eIReflector.GetValue(selectedItem, chListBox.PropertyName))) 
          chListBox.SelectedItems.Add(item); 
        } 
       } 
      } 
     } 

     protected virtual void CheckListBox_SelectionChanged(object sender, SelectionChangedEventArgs e) 
     { 
      foreach (object addedItem in e.AddedItems) 
      { 
       if (!SelectedItems.Contains(addedItem)) 
        SelectedItems.Add(addedItem); 

       if (!SelectedSource.Contains(addedItem)) 
        SelectedSource.Add(addedItem); 
      } 

      foreach (object removedItem in e.RemovedItems) 
      { 
       this.SelectedItems.Add(removedItem); 
       this.SelectedSource.Remove(removedItem); 
      } 
     } 
    } 

回答

5

嘗試使用

chListBox.ClearSelection(); // For C# 

chListBox.UnselectAll(); // For WPF 

它工作非常適合我...

+0

在這裏工作爲好,.ClearSelection()完​​美的作品,Upvoted –

+0

在WPF列表已不是法ClearSelection()。你知道另一種方式嗎? – MasterMedia

+1

-1。所有UnselectAll()都會取消選擇列表框中的選定項目。您的回答對WPF無效,這是OP要求的。 – WillB3

1

我能做到這一點在WPF執行以下操作:

while(chListBox.SelectedItems.Count > 0) 
{ 
    chListBox.Items.Remove(chListBox.SelectedItem); 
} 
0

我能夠得到它與以下工作:

var listboxItem = m_listBoxAutocomplete.FindChildElements<ListBoxItem>().FirstOrDefault(t => t.IsSelected); 
    Keyboard.Focus(listboxItem); 

然而,你將需要谷歌FindChildElements extention這是現成的