将组合框的选定项和项列表绑定到 wpf 中的不同属性
问题:如何将 ComboBox 的选定项目的绑定设置为一个属性,并将项目列表设置为另一属性?
信息:
我正在为我的公司制定一个库存计划,该计划主要针对那些收到货物的人。我已经进行了一些设置,以便在屏幕的一侧有一个货物列表。当他们选择其中一项时,将显示货件的所有信息,以便可以对其进行编辑。一点信息是接收货物的人。我希望这样当他们单击发货时,显然收到发货的用户就是在组合框中弹出的用户。但我希望组合框包含所有其他用户的列表。
如果不是因为我要从数据库中提取用户列表,这并不难。列表框有一个数据模板,用于从用户数据类型列表中提取正确的信息。到目前为止我所尝试的是在组合框为其列表绑定的视图模型中拥有一个集合视图,然后是一个单独的属性,它是与发货数据类型一起提供的单个用户实例。
我根据 MV-VM 编程模型使用 Visual Basic 和 XAML 进行编程。
Question: How do you set the binding for a ComboBox's Selected items to one property and the items list to a different property?
Info:
I have an inventory program I'm working on for my company which is aimed mostly at the people who receive shipments as they come in. I have things set up so that there is a list of shipments on one side of the screen. when they select one of these all the information on the shipment is displayed so it can be edited. one bit of information is the person who received the shipment. I want it so that when they click the shipment, obviously the user who received the shipment is the one who pops up in the combo box. but i want the combo box to contain the list of all the other users.
This wouldn't be to hard if it weren't for the fact that I'm pulling the list of users from a database. the list box has a data template for pulling the proper information out of the list of user data types. what I've tried so far is to have a collection view in the viewmodel that the combo box binds to for its list, then a seperate property which is a single user instance that comes with the shipment data type.
I'm programing in Visual Basic and XAML according to the M-V-VM programing model.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的做法没有任何问题。 ViewModel 可以公开来自数据库的类型集合以及绑定到所选项目的单独属性,例如:
就组合框为空而言,
另外 - 如果您希望组合框显示货件对象中的用户,请记住确保您的虚拟机(或其基础)实现了 INotifyPropertyChanged,并在 SelectedShipment 中引发 PropertyChanged 事件 setter - 这将通过 DataBinding 将更改传播到视图...
希望这有帮助:)
There is nothing wrong with your approach. The ViewModel can expose a collection of types from the db and a separate property bound to the selected item, something like:
In terms of the combobox being empty
Also - if you want the combo-box to display the User from the shipment object, then remember to ensure your VM (or its base) implements
INotifyPropertyChanged
, and raises a PropertyChanged event in theSelectedShipment
setter - this will propagate the change through to the View via the DataBinding...hope this helps :)
假设您使用 Linq-to-SQL,我将回答这个问题:
这是一个常见的 PITA 问题,其中
Shipment
对象中的User
属性包含一个引用到与填充ComboBox
的查询中的User
对象不同的User
对象,即使它们代表相同的记录。为了克服这个问题,您可能需要将用户
ComboBox
绑定到从与Shipments
列表相同的DataContext
检索的用户列表。 。例子:
I'll be answering this question assuming you're using Linq-to-SQL:
This is a common PITA problem, where the
User
property in yourShipment
object contains a reference to aUser
object that is different from theUser
object from the query that populated theComboBox
, even though they represent the same record.To overcome this, you might want to bind the users
ComboBox
to a list of users that is retrieved from the sameDataContext
as the list ofShipments
.Example: