ComboBox wpf 未选择项目
我正在尝试将组合框绑定到对象列表,并且效果很好,除了选定的值之外,我还缺少什么吗?
<ComboBox ItemsSource="{Binding OrderInfoVm.AllCountries}"
SelectedValuePath="country_code" DisplayMemberPath="country_name"
SelectedValue="{Binding OrderInfoVm.BillingCountry}" />
基本上,我想将值绑定到国家/地区代码,并将选定的值设置为绑定到 OrderInfoVm.BillingCountry (实现 INotifyPropertyChanged)的国家/地区代码
。最初,当控件加载时,选定的值是空的,但单击时会填充 BillingCountry。选择的值似乎没有改变。我该如何补救?
I am trying to bind a combo box to a list of objects, and it works great, besides the selected value, am I missing somethign?
<ComboBox ItemsSource="{Binding OrderInfoVm.AllCountries}"
SelectedValuePath="country_code" DisplayMemberPath="country_name"
SelectedValue="{Binding OrderInfoVm.BillingCountry}" />
Basically I want to bind value to country codes and set the selected value to the country code bound to OrderInfoVm.BillingCountry (which implements INotifyPropertyChanged)
Initially when the control loads selected value is empty, but on click BillingCountry is populated. Selected value does not seem to change. How can I remedy that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我确实同意 Alex 的观点,即使用 SelectedItem 可以提供所需的行为。请参阅下面的代码。它有效并且希望能进一步帮助您:
I do agree with Alex that using SelectedItem gives the desired behaviour. See the code below. It works and will hopefully help you further:
也许您正在尝试实现与此类似的东西: Bound ComboBox
Maybe you are trying to implement something similar to this: Bound ComboBox
尝试将其更改为 SelectedItem 并设置 Mode=TwoWay...
编辑:您可能不需要将其更改为 SelectedItem,也许只需设置 TwoWay 就可以了,但这就是我在自己的代码中完成的方式。
Try changing it to SelectedItem and set Mode=TwoWay...
Edit: You may not need to change it to SelectedItem, perhaps just setting TwoWay will work, but that is how I've done it in my own code.
请确保您指定了正确的绑定路径。
尝试在调试模式下启动项目并查看输出窗口以查看是否存在任何绑定错误
Please ensure that you've specified correct binding path.
Try starting project in debug mode and look at the output window to see if there are any binding errors
试一试;我相信您将 SelectedValuePath 和 SelectedValue 混合在一起:
供参考:
ItemsSource = 获取或设置用于生成 ItemsControl (ComboBox) 内容的集合。
SelectedValue = 获取或设置使用SelectedValuePath 获取的SelectedItem 的值。
SelectedValuePath = 获取或设置一个值,该值指示用于从 SelectedItem 获取 SelectedValue 的路径。
DisplayMemberPath = 获取或设置源对象上的值的路径,以用作对象的视觉表示。
Give this a shot; I believe you have your SelectedValuePath and SelectedValue mixed up:
For Reference:
ItemsSource = Gets or sets a collection used to generate the content of the ItemsControl (ComboBox).
SelectedValue = Gets or sets the value of the SelectedItem, obtained by using SelectedValuePath.
SelectedValuePath = Gets or sets a value that indicates the path used to get the SelectedValue from the SelectedItem.
DisplayMemberPath = Gets or sets a path to a value on the source object to serve as the visual representation of the object.