c# - comboBox.comboSpec.SelectedItem.ToString() 没有给出正确的值

标签 c# wpf web-services data-binding combobox

我将组合框绑定(bind)到网络服务发送的用户集合。

当我尝试使用以下方式读取所选值时:

string x = comboSpec.SelectedItem.ToString();

or 

string x = comboSpec.SelectedValue.ToString();

我得到(断点模式):

Name | Value                          | Type

x    | "UI.WebServiceReference.Users" | string

如何从组合框中获取选定的值(如“admin”)而不是绑定(bind)元素的类型?

最佳答案

如果您要显示 DisplayMemberPath 并在 ComboBox 定义中使用 SelectedValuePath 将名称设置为您的值,那么您可以使用 SelectedValue 而不是 SelectedItem。 SelectedItem 返回 ItemsCollection 中的项目,而 SelectedValue 返回项目绑定(bind)的属性。

这是一个例子:

<ComboBox Name="employees"
         ItemsSource="{Binding Source={StaticResource Employees}}"
         ItemTemplate="{StaticResource EmployeeItemTemplate}"
         SelectedValuePath="EmployeeUsername"/>

关于c# - comboBox.comboSpec.SelectedItem.ToString() 没有给出正确的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8494235/

相关文章:

c# - 根据文件填充一组复选框

c# - 无法用逗号 ( , ) 保存号码

c# - 为什么网络异常是由 System.Exception 类型的 Windows.Web.Http.HttpClient 引发的,而不是更具体的异常?

c# - 如何防止使用 Newtonsoft JSON 序列化 DependencyObjects 上的 DependencyObjectType 信息?

java - 为什么 MessageContext 中的 HttpServletRequest 为 null?

c# - 列表数据结构C#效率

c# - 以编程方式更改 WPF Datagrid 行背景颜色

wpf - 属性更改后 DataTrigger 未重新评估

java - Android webservice访问错误

java - 将 CXF 服务的客户端 receiveTimeout 设置为 4 分钟以上