WPF 列表框绑定
我有一个医生对象,其属性之一是诊所的 ObservableList
。它被用在一个窗口中来显示医生的详细信息。我可以获取要绑定到 TextBox
和 ComboBox
控件的各个属性,但无法获取要绑定到 ListBox
的诊所列表。
这是我的 ListBox
的 xaml:
<ListBox Height="318"
HorizontalAlignment="Left"
Margin="422,0,0,0"
Name="lbClinic"
VerticalAlignment="Top"
Width="158"
SelectedValue="{Binding ClinicID, Path=Clinics, Mode=TwoWay,
UpdateSourceTrigger = PropertyChanged}"
SelectedValuePath="ClinicID"
DisplayMemberPath="Name"
ItemsSource="{Binding DataContext.ClinicList,
ElementName = PhysicianInfoLookup, Mode = OneWay}"
SelectionMode="Multiple" />
列表框正确填充了 ClinicList 中的项目,ClinicList 是所有可能的诊所的列表。但是,我无法从要绑定的医生对象获取诊所列表,以便在列表框中选择它的项目。我还想走另一种方式,如果取消选择某个项目,医生对象中的 ObservableList 将相应更改。
如何将医生对象中的诊所 ObservableList 双向绑定到列表框中的诊所列表(诊所对象的 ObservableList)?
谢谢。
I have a physician object, and one of its properties is an ObservableList
of clinics. It is being used in a window to show the details of a physician. I can get individual properties to bind to TextBox
and ComboBox
controls, but I can't get the list of clinics to bind to my ListBox
.
Here is the xaml for my ListBox
:
<ListBox Height="318"
HorizontalAlignment="Left"
Margin="422,0,0,0"
Name="lbClinic"
VerticalAlignment="Top"
Width="158"
SelectedValue="{Binding ClinicID, Path=Clinics, Mode=TwoWay,
UpdateSourceTrigger = PropertyChanged}"
SelectedValuePath="ClinicID"
DisplayMemberPath="Name"
ItemsSource="{Binding DataContext.ClinicList,
ElementName = PhysicianInfoLookup, Mode = OneWay}"
SelectionMode="Multiple" />
The Listbox populates properly with items from the ClinicList which is a list of all possible clinics. However, I cannot get the Clinics list from the physician object to bind so that it's items are selected in the Listbox. I also want to go the other way and if an item is deselected, the ObservableList in the physician object will change accordingly.
How do I two-way bind the ObservableList of Clinics in my physician object to the list of Clinics (ObservableList of clinic objects) in my Listbox?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
迈克,你的绑定几乎没有问题。
这是一个完整的示例,演示了执行(我认为)您所追求的操作的一种方法。
视图:
视图模型:
请注意,对于读/写属性,您可能希望引发属性更改通知。
Mike, there are few problems with your bindings.
Here's a complete sample demonstrating one way of doing what (I think) you're after.
View:
View model:
Note that for read/write properties you would probably want to raise property change notifications.
您的问题是
SelectedValue
。在ListBox
级别,不支持绑定到多个选择对象。使用绑定执行此操作的唯一真正方法是重新设计您的 ViewModel,以便从绑定返回的诊所列表代表所有诊所,并且其中的每个对象都应该有一个IsSelected
(或类似的)财产。然后,您可以通过在
ListBox
节点中添加此 XAML,使用样式来处理多重选择:Your issue is
SelectedValue
. At theListBox
level, binding to multiple selection objects is not supported. The only real way to do this with bindings would be to rework your ViewModel so that the list of clinics returned from the binding represents all clinics, and each object there should have anIsSelected
(or something similar) property.You can then use a style to handle the multi selection by adding this XAML within your
ListBox
node:您将需要使用一个模板,其中
TextBox
作为名称,ListBox
作为诊所,并且只需将内部ListBox
路径绑定到诊所即可。DisplayMemberPath
是单个TextBox
的快捷方式。如果您想要更多,那么您需要单独的控制。You are going to need to use a template with
TextBox
for name andListBox
for Clinics and you just bind the internalListBox
path to Clinics.DisplayMemberPath
is a short cut a singleTextBox
. If you want more then you need individual controls.