简体   繁体   English

如何在C#中将selectedValue设置为Controls.Combobox?

[英]How to set selectedValue to Controls.Combobox in c#?

I have a combobox and I see that I am not able to set SelectedValue like this: 我有一个组合框,看到无法设置SelectedValue:

cmbA.SelectedValue = "asd"

So I tried to do this 所以我尝试这样做

cmbA.SelectedIndex = cmbA.FindString("asd");

Based on How to set selected value from Combobox? 基于如何设置从组合框选择的值?

I realised that my combobox is a System.Windows.Controls.ComboBox and not a System.Windows.Forms.ComboBox. 我意识到我的组合框是System.Windows.Controls.ComboBox,而不是System.Windows.Forms.ComboBox。

That means that FindString() is not available. 这意味着FindString()不可用。

Based on User Control vs. Windows Form I get that forms are the container for controls, but I dont get why the Controls.ComboBox does not implement FindString(). 基于用户控件和Windows窗体,我知道窗体是控件的容器,但是我不明白为什么Controls.ComboBox不实现FindString()。

Do I have to write my own code to do what FindString() does for Forms.ComboBox? 我是否需要编写自己的代码来完成Finds()对Forms.ComboBox的操作?

WPF ComboBoxes are not the same as WinForms ones. WPF组合框与WinForms不同。 They can display a collection of objects, instead of just strings. 它们可以显示对象的集合,而不仅仅是字符串。

Lets say for example if I had 比方说,如果我有

myComboBox.ItemsSource = new List<string> { "One", "Two", "Three" };

I could just use the following line of code to set the SelectedItem 我可以使用下面的代码行设置SelectedItem

myComboBox.SelectedItem = "Two";

We're not limited to just strings here. 我们不仅限于这里的字符串。 I could also say I want to bind my ComboBox to a List<MyCustomClass> , and I want to set the ComboBox.SelectedItem to a MyCustomClass object. 我也可以说我想将ComboBox绑定到List<MyCustomClass> ,并且要将ComboBox.SelectedItem设置为MyCustomClass对象。

For example, 例如,

List<MyCustomClass> data = new List<MyCustomClass> 
{ 
    new MyCustomClass() { Id = 1, Name = "One" },
    new MyCustomClass() { Id = 2, Name = "Two" },
    new MyCustomClass() { Id = 3, Name = "Three" }
};
myComboBox.ItemsSource = data;
myComboBox.SelectedItem = data[0];

I could also tell WPF I want to consider the Id property on MyCustomClass to be the identifying property, and I want to set MyCombbox.SelectedValue = 2 , and it will know to find the MyCustomClass object with the .Id property of 2, and set it as selected. 我还可以告诉WPF,我想将MyCustomClass上的Id属性视为标识属性,并且我想设置MyCombbox.SelectedValue = 2 ,它将知道找到.Id属性为2的MyCustomClass对象,并设置它被选中。

myComboBox.SelectedValuePath = "Id";
myComboBox.SelectedValue = 2;

I could even set the Display Text to use a different property using 我什至可以设置Display Text以使用其他属性

myComboBox.DisplayMemberPath = "Name";

To summarize, WPF ComboBoxes work with more than just Strings, and because of the expanded capabilities, FindString is not needed. 总而言之,WPF ComboBoxes不仅可用于字符串,而且由于功能已扩展,因此不需要FindString。 What you are most likely looking for is to set the SelectedItem to one of the objects that exist in your ItemsSource collection. 您最可能要寻找的是将SelectedItem设置为ItemsSource集合中存在的对象之一。

And if you're not using ItemsSource, then a standard for-each loop should work too 而且,如果您不使用ItemsSource,那么标准的for-each循环也应该工作

foreach(ComboBoxItem item in myComboBox.Items)
{
    if (item.Content == valueToFind)
        myComboBox.SelectedItem = item;
}

I don't know what you are trying to do but I think it would be easier to just do 我不知道您要做什么,但是我认为这样做会更容易

cmbA.Text = "String";

That way you get your selected item 这样您就可以得到您选择的物品

Else I found an intersting article that could help you out: Difference between SelectedItem, SelectedValue and SelectedValuePath 另外,我发现了一篇有趣的文章可以为您提供帮助: SelectedItem,SelectedValue和SelectedValuePath之间的区别

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM