XAML 中的 Comboxbox 选定值

发布于 2024-07-25 15:53:53 字数 132 浏览 6 评论 0原文

如何设置组合框的选定值。 例如,我有 1-10 之间的一些值,我想选择第 5 个值。

我尝试使用 SelectedValueSelectedItem,但它对我不起作用。

How to set the selectedvalue for combobox. For example I have some values from 1-10 and I want to select the 5th value.

I tried using SelectedValue and SelectedItem, but its not working for me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岁月如刀 2024-08-01 15:53:53

SelectedIndex 应该可以工作。

SelectedIndex should work.

雨的味道风的声音 2024-08-01 15:53:53

如果要选择“第五个值”,则应使用 SelectedIndex = 4(因为索引是从零开始的)。 如果要选择“值 5”,则应使用 SelectedValue = 5。

If you want to select "the fifth value", you should use SelectedIndex = 4 (since indexes are zero-based). If you want to select "the value 5", you should use SelectedValue = 5.

樱花坊 2024-08-01 15:53:53

WPF 中的 ComboBox 可以通过 3 种方式选择项目。
第一个是设置 SelectedIndex,您可以在其中为项目设置从零开始的索引。
另一种方法是使用 SelectedValue 设置值。 您必须在此处添加所选对象的值。 如果您有 3 个组合框项目{你好,再见,早上好}。 假设您想选择“bye”,则必须设置 SelectedValue =“bye”。
最后一种方法是通过SelectedItem设置选定的对象。 您必须在那里设置 ComboBoxItem 或 XmlElement。 您必须知道组合框可以包含 ComboBoxItems 和 XmlElements。 行为有点不同,但如果您使用组合框一段时间,您就会理解它。

希望这可以帮助。

A ComboBox in WPF can select items in 3 ways.
The first is to set SelectedIndex, where you set the zero-based index to an item.
Another way is to to set a value with SelectedValue. Here you must add the value of the object you have selected. F.e. you have 3 comboboxitems {hello, bye, good morning}. So lets say you want to select bye you have to set SelectedValue = "bye".
The last way ist to set the selected object by SelectedItem. There you have to set an ComboBoxItem or an XmlElement. You have to know that a combobox can contain ComboBoxItems and XmlElements. The behaviour is a bit differnt, but if you work with combobox some time you will understand it.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文