Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# can';t清除ListBox.SelectedItems集合wpf_C#_Wpf_Listbox - Fatal编程技术网

C# can';t清除ListBox.SelectedItems集合wpf

C# can';t清除ListBox.SelectedItems集合wpf,c#,wpf,listbox,C#,Wpf,Listbox,我无法清除ListBox.SelectedItems集合。请说明我做错了什么。 我以不同的方式清除集合,但它保留了以前的集合 我希望: chListBox.SelectedItems.Clear(); 或 或 我的代码: public class CheckListBox : ListBox { public CheckListBox() { this.SelectionChanged += CheckListBox_Select

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

我希望:

chListBox.SelectedItems.Clear();

我的代码:

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#


它对我来说工作得非常完美…

我能够在WPF中执行以下操作:

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

我能够通过以下方式使其工作:

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

但是,您需要使用google FindChildElements扩展,该扩展也可以在这里使用,.ClearSelection()工作正常,UpvotedIn WPF listBox没有方法ClearSelection()。你知道其他的方法吗?-1。UnselectAll()所做的全部工作是取消选择列表框中的选定项。您的答案不适用于WPF,这正是OP所要求的。也许可以在WPF解决方案中添加一条注释,告知人们它将触发一个新的
SelectionChanged
事件。
chListBox.UnselectAll(); // For WPF
while(chListBox.SelectedItems.Count > 0)
{
     chListBox.Items.Remove(chListBox.SelectedItem);
}
    var listboxItem = m_listBoxAutocomplete.FindChildElements<ListBoxItem>().FirstOrDefault(t => t.IsSelected);
    Keyboard.Focus(listboxItem);