WPF ComboBoxItem中是否有等效的值? [英] Is there an equivalent of value in a WPF ComboBoxItem?

查看:79
本文介绍了WPF ComboBoxItem中是否有等效的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我只是在这里遗漏了一些明显的东西,但是我找不到任何地方需要的东西.我想做的是用ComboBoxItems手动填充ComboBox.在这些ComboBoxItem上,我想设置一个显示字符串和一个整数,该整数是在幕后使用的关键.我将字符串设置为ComboBoxItem.Content,它没有显示任何问题.但是我应该在哪里设置整数?我尝试了我能想到的每个字段,但是当我从下拉列表中选择某些内容然后检查调试器中的值时,它们都不起作用,ComboBox上的SelectedValue始终为null.我曾经使用过的每个组合框都支持此功能,很明显,如果WPF与SelectedValuePath属性进行数据绑定,则WPF会这样做.但是,如何在不进行数据绑定的情况下使它工作呢?

I hope I'm just missing something obvious here, but I cannot find what I need anywhere.  What I want to do is manually populate a ComboBox with ComboBoxItems.  On those ComboBoxItems, I want to set a string for display and an integer that is the key for use behind the scenes.  I set the string to ComboBoxItem.Content, and it displays no problem.  But where do I set the integer?  I have tried every field I can think of, and none of them work, when I pick something from the drop down and then check the values in the debugger, the SelectedValue on the ComboBox is always null.  Every combo box I have ever used has supported this, and obviously, the WPF one does if it is databound with the SelectedValuePath property.  But how can I get this to work without databinding?

推荐答案

创建一个存储字符串及其字符串的类.将ComboBoxItem.Content设置为该类的实例.将DisplayMemberPath设置为字符串属性名称,并将SelectedValuePath设置为int属性名称.

Create a class which stores the string and it.  Set the ComboBoxItem.Content to an instance of that class.  Set the DisplayMemberPath to the string property name and the SelectedValuePath to the int property name.

编辑----------------------

EDIT ----------------------

没关系,直到我再次阅读您的帖子,我才注意到您不想进行数据绑定.我的解决方案需要绑定.

Nevermind, I didn't notice that you don't want to databind until I read your post again.  My solution requires binding.


这篇关于WPF ComboBoxItem中是否有等效的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆