清除 WPF 中的组合框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19362911/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:51:09  来源:igfitidea点击:

Clear a combobox in WPF

c#wpfcombobox

提问by user2631662

How do I clear a combobox in WPF? I have tried this code:

如何清除 WPF 中的组合框?我试过这个代码:

 private void btClear1_Click(object sender, RoutedEventArgs e)
    {

        txtID.Text = String.Empty;
        this.cbType.SelectedItem = -1;
    }

采纳答案by Kevin DiTraglia

To clear the selection set the SelectedIndexnot the SelectedItem

要清除选择集而SelectedIndex不是SelectedItem

cboType.SelectedIndex = -1;

You can set the SelectedItemor SelectedValueas well, but change it to nullinstead of -1 (these point to an object not an integer).

您也可以设置SelectedItemor SelectedValue,但将其更改为null而不是 -1(这些指向对象而不是整数)。

cboType.SelectedItem = null;

回答by Fred

cbTypion.SelectedItem = -1to clear the selection cbType.Items.Clear()to clear all the items

cbTypion.SelectedItem = -1清除选择 cbType.Items.Clear()以清除所有项目

回答by Sabareeshwari Kannan

You can reset the combo box by binding in the XAML page.

您可以通过在 XAML 页面中绑定来重置组合框。

For example, in that combobox field in the XAML page:

例如,在 XAML 页面的组合框字段中:

text={Binding viewmodelobject Name.property Name}

And then in ViewModelPage:

然后在ViewModelPage

viewmodelobject Name.property Name="";

回答by vapcguy

Totally clearing box items
For Googlers, since the title is misleading, if we were talking clearing the items from the box, I've seen several answers saying to use cbType.Items.Clear(). It depends on how the items were loaded. You could have hard-coded them into the XAML, added them with a function dynamically at runtime, used a type of databinding and/or loaded them to the .ItemSource. It will work for all but the latter case.

完全清除盒子里的物品
对于谷歌员工来说,由于标题具有误导性,如果我们谈论的是从盒子里清除物品,我已经看到几个答案说使用cbType.Items.Clear(). 这取决于物品的加载方式。您可以将它们硬编码到 XAML 中,在运行时动态添加一个函数,使用一种数据绑定和/或将它们加载到.ItemSource. 除了后一种情况,它适用于所有其他情况。

When you are using the .ItemSourceto load the ComboBoxvia a DataTable's DefaultView, for example, you cannot simply do cbType.Items.Clear(). Since the method of populating the dropdown was not included in the question, I submit that for when you set the .ItemSource, you must do:

例如,当您通过 DataTable使用.ItemSource加载 时,您不能简单地执行. 由于问题中未包含填充下拉列表的方法,因此我提出,当您设置 时,您必须执行以下操作:ComboBoxDefaultViewcbType.Items.Clear().ItemSource

cbType.ItemsSource = null;

cbType.ItemsSource = null;

instead. Otherwise, if you try cbType.Items.Clear()you will get:

反而。否则,如果您尝试,cbType.Items.Clear()您将获得:

Operation is not valid while ItemSource is in use. Access and modify 
elements with ItemsControl.ItemsSource instead


Clearing the selected item
I went back and saw the OP's comment, saying the wish was to clear the selection, not the box. For that, the other answers stand:


清除所选项目
我回去看到OP的评论,说希望清除选择,而不是框。为此,其他答案是:

cbType.SelectedIndex = -1;  
cbType.Text = "";  // I've found the first line does this for me, as far as appearances, but found other postings saying it's necessary - maybe they were using the property and it wasn't getting cleared, otherwise