无法清除 ListBox.SelectedItems 集合 wpf
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19362492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
can't clear ListBox.SelectedItems collection wpf
提问by MasterMedia
I can't clear ListBox.SelectedItems collection. Please suggest what I'm doing wrong. I clear collection in different ways, but it leave previous collection.
我无法清除 ListBox.SelectedItems 集合。请建议我做错了什么。我用不同的方式清除收藏,但它离开以前的收藏。
I clear collection so:
我清除收集所以:
chListBox.SelectedItems.Clear();
or
或者
chListBox.UnselectAll();
or
或者
chListBox.SetSelectedItems(new ArrayList());
My code:
我的代码:
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);
}
}
}
回答by Microsoft DN
Try using
尝试使用
chListBox.ClearSelection(); // For C#
and
和
chListBox.UnselectAll(); // For WPF
It is working perfect for me...
它对我来说非常完美......
回答by WillB3
I was able to do it in WPF doing the following:
我能够在 WPF 中执行以下操作:
while(chListBox.SelectedItems.Count > 0)
{
chListBox.Items.Remove(chListBox.SelectedItem);
}
回答by RJ Thompson
I was able to get it working with the following:
我能够使用以下方法使其工作:
var listboxItem = m_listBoxAutocomplete.FindChildElements<ListBoxItem>().FirstOrDefault(t => t.IsSelected);
Keyboard.Focus(listboxItem);
However you will need to google FindChildElements extention which is readily available
但是,您需要谷歌 FindChildElements 扩展,这是现成的

