设置 ComboBoxItem 的文本和值

发布于 2024-08-09 22:31:34 字数 393 浏览 3 评论 0原文

我正在尝试以编程方式填充组合框。我正在创建 ComboBoxItems 并希望设置它们的文本(对最终用户可见的文本)和它们的值(用户选择它后我将在后台处理的对象。

但是 ComboBoxItem 似乎只具有一个成员满足这两个要求:同时,这不符合我的需求,因为我想区分文本和值属性,并且希望在没有数据绑定的情况下做到这一点

?当前代码如下所示:

ComboBox comboBox;
ComboBoxItem item = new ComboBoxItem();
item.Content = "First Item";
item.Value = 1; // Does not work, no such member as Value!
comboBox.Items.Add(item);

I'm trying to populate a ComboBox programatically. I am creating ComboBoxItems and would like to set their text (the text that is visible for the end-user) and their value (the object that I will handle in the background after the user has selected it.

However the ComboBoxItem seems to only have one member for these two requirements: the Content variable. At the same time this would not fit my needs as I want to distinguish the text and value properties and want to do this without data binding. Is there some viable solution to achieve this?

My current code looks as follows:

ComboBox comboBox;
ComboBoxItem item = new ComboBoxItem();
item.Content = "First Item";
item.Value = 1; // Does not work, no such member as Value!
comboBox.Items.Add(item);

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

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

发布评论

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

评论(1

苯莒 2024-08-16 22:31:34

我猜你可以使用 Tag 属性。

Guess you can use the Tag property.

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