c# - 无法清除 ListBox.SelectedItems 集合 wpf

标签 c# wpf listbox

我无法清除 ListBox.SelectedItems 集合。请建议我做错了什么。 我以不同的方式清除集合,但它保留了以前的集合。

我清除集合是这样的:

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);
            }
        }
    }

最佳答案

尝试使用

chListBox.ClearSelection(); // For C#

chListBox.UnselectAll(); // For WPF

它非常适合我...

关于c# - 无法清除 ListBox.SelectedItems 集合 wpf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19362492/

相关文章:

wpf - 如何用 ListBox 上的 ContentPresenter 替换 ScrollViewer?

c# - Datagridview全行选择但获取单个单元格值

C# 数据适配器参数

wpf - 如何使 WPF wrappanel 子项拉伸(stretch)?

WPF XAML WrapPanel 连续列表框项目

jquery - 如何创建一个空列表框并传递选项而不选择它们?

c# - C# 中的私有(private)静态变量和线程安全

c# - ASP.NET 核心 2.0 独立 : passing listening url via command line

c# - WPF和MVVM模型与viewModel分离

wpf - 将 DataGrid 行中的 DoubleClick 命令绑定(bind)到 VM