我将组合框绑定(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/