.net 在 WPF ComboBox 中查找项目

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

Find item in WPF ComboBox

.netwpfsearchuser-controls

提问by Dillie-O

I know in ASP.NET I can get an item from a DropDownList by using

我知道在 ASP.NET 中我可以通过使用从 DropDownList 中获取一个项目

DropDownList1.Items.FindByText

Is there a similar method I can use in WPF for a ComboBox?

我可以在 WPF 中为 ComboBox 使用类似的方法吗?

Here's the scenario.

这是场景。

I have a table called RestrictionFormat that contains a column called RestrictionType, the type is a foreign key to a table that stores these values.

我有一个名为 RestrictionFormat 的表,其中包含一个名为 RestrictionType 的列,该类型是存储这些值的表的外键。

In my editor application I'm writing, when the user selects the RestrictionFormat from a ComboBox (this works fine), I'm pulling up the details for editing. I'm using a second ComboBox to make sure the user only selects one RestrictionType when editing. I already have the second combobox bound property from the RestrictionType table, but I need to change the selected index on it to match the value specified in the record.

在我正在编写的编辑器应用程序中,当用户从 ComboBox 中选择 RestrictionFormat(这很好用)时,我会提取详细信息进行编辑。我使用第二个 ComboBox 来确保用户在编辑时只选择一个 RestrictionType。我已经有了 RestrictionType 表中的第二个组合框绑定属性,但我需要更改其上的选定索引以匹配记录中指定的值。



Here's the scenario.

这是场景。

I have a table called RestrictionFormat that contains a column called RestrictionType, the type is a foreign key to a table that stores these values.

我有一个名为 RestrictionFormat 的表,其中包含一个名为 RestrictionType 的列,该类型是存储这些值的表的外键。

In my editor application I'm writing, when the user selects the RestrictionFormat from a ComboBox (this works fine), I'm pulling up the details for editing. I'm using a second ComboBox to make sure the user only selects one RestrictionType when editing. I already have the second combobox bound property from the RestrictionType table, but I need to change the selected index on it to match the value specified in the record.

在我正在编写的编辑器应用程序中,当用户从 ComboBox 中选择 RestrictionFormat(这很好用)时,我会提取详细信息进行编辑。我使用第二个 ComboBox 来确保用户在编辑时只选择一个 RestrictionType。我已经有了 RestrictionType 表中的第二个组合框绑定属性,但我需要更改其上的选定索引以匹配记录中指定的值。

Does this make sense?

这有意义吗?

回答by Rich

Can you use ItemContainerGenerator?

您可以使用 ItemContainerGenerator 吗?

ItemContainerGenerator contains a ContainerFromItem method that takes an object parameter. If you have a reference to the full object that your comboBox contains (or a way to reconstruct it), you can use the following:

ItemContainerGenerator 包含一个采用对象参数的 ContainerFromItem 方法。如果您有对组合框包含的完整对象的引用(或重构它的方法),您可以使用以下内容:

ComboBoxItem item = 
    (ComboBoxItem)myComboBox.ItemContainerGenerator.ContainerFromItem(myObject);

回答by aku

In WPF you can use FindName method.

在 WPF 中,您可以使用 FindName 方法。

XAML:

XAML:

    <ComboBox Name="combo">
        <ComboBoxItem Name="item1" >1</ComboBoxItem>
        <ComboBoxItem Name="item2">2</ComboBoxItem>
        <ComboBoxItem Name="item3">3</ComboBoxItem>
    </ComboBox>

Code-behind file

代码隐藏文件

   item1.Content = "New content"; // Reference combo box item by name
   ComboBoxItem item = (ComboBoxItem)this.combo.FindName("item1"); // Using FindName method

To find item by its content you can use UI automation.

要按内容查找项目,您可以使用UI 自动化

回答by SmartyP

instead of trying to bind the SelectedIndex why don't you just bind the SelectedItem in the ComboBox to the value in the record?

而不是尝试绑定 SelectedIndex 为什么不将 ComboBox 中的 SelectedItem 绑定到记录中的值?

in other words, set the DataContext of the ComboBox (or its parent) to the selected 'record' and bind the SelectedItem on the ComboBox to an exposed property on the 'record'..

换句话说,将 ComboBox(或其父级)的 DataContext 设置为选定的“记录”,并将 ComboBox 上的 SelectedItem 绑定到“记录”上的公开属性。

it may help if you could provide some code snippets, or extra details so that responses can be more specific and refer to the variables and types you are using in both the source record and the ComboBox which you have populated.

如果您可以提供一些代码片段或额外的详细信息,以便响应可以更具体并引用您在源记录和已填充的 ComboBox 中使用的变量和类型,这可能会有所帮助。

回答by Arcturus

You can retrieve combobox items in two ways:

您可以通过两种方式检索组合框项目:

By item:

按项目:

ComboBoxItem item = (ComboBoxItem) control.ItemContainerGenerator.ContainerFromItem(control.SelectedItem);

By index:

按索引:

ComboBoxItem item = (ComboBoxItem) control.ItemContainerGenerator.ContainerFromIndex(1);

回答by Denis Troller

Can you give some context as to what exactly you are trying to do?

你能给出一些关于你到底想要做什么的背景吗?

What objects do you put in your Combobox, and using which method? (Are you setting or binding the ItemsSource property?) Why do you need to lookup an item by its "text"? The most usual usage in WPF is to bind the SelectedItem property to something else so you can retrieve/set the selected entry using your representation. Is there a specific requirement for which you need to find a specific item in the list?

您将哪些对象放入 Combobox,并使用哪种方法?(您是在设置还是绑定 ItemsSource 属性?) 为什么需要通过“文本”来查找项目?WPF 中最常见的用法是将 SelectedItem 属性绑定到其他内容,以便您可以使用您的表示检索/设置所选条目。是否存在需要在列表中查找特定项目的特定要求?

Worst case, you can perform the search on the collection to which you bind your ComboBox using Linq To Objects.

最坏的情况是,您可以使用 Linq To Objects 对绑定 ComboBox 的集合执行搜索。

Do not mistake the ComboBoxItem (that is, the element generated for you behind the scenes by WPF when you bind ItemsSource) with the SelectedItem, which is the actual object in the collection you bind to. That usually is the source of most problems whith WPF when you are not used to it. There are precious few cases when you need to find the actual ComboBoxItem.

不要将 ComboBoxItem(即绑定 ItemsSource 时 WPF 在幕后为您生成的元素)与 SelectedItem 混淆,后者是您绑定到的集合中的实际对象。当您不习惯 WPF 时,这通常是大多数问题的根源。需要查找实际 ComboBoxItem 的情况很少。