一个 ComboBox 选项可以绑定到两个不同的数据吗?
我不确定这是否可能。我创建了一个像这样的组合框...
<ComboBox Name="testType"
Margin="0,5,0,0"
IsTextSearchEnabled="True"
DisplayMemberPath="Description"
SelectedValue="{Binding MyClass.Id, Mode=TwoWay}"
SelectedValuePath="Id"/>
后面的代码加载可用选项...
DataTable testCatList = TestTypeBase.GetAll();
testType.ItemsSource = testCatList.DefaultView;
以便它正确显示 MyClass 中的所有项目。当用户做出选择时,MyClass 中的 Id 字段将按预期更新。伟大的。
这是我的问题:我的 testCatList 包含每行的 Id 和说明,我希望这两个字段都绑定到当前的 MyClass 实例。所以这就是我尝试过的:
<ComboBox Name="testType"
Margin="0,5,0,0"
IsTextSearchEnabled="True"
DisplayMemberPath="Description">
<ComboBox.SelectedValue>
<MultiBinding Converter="{???}">
<Binding Path="MyClass.Id" Mode="TwoWay"/>
<Binding Path="MyClass.Description" Mode="TwoWay"/>
</MultiBinding>
</ComboBox.SelectedValue>
</ComboBox>
在这里,我希望将 MyClass.Id 设置为选定的 Id,将 MyClass.Description 设置为选定的描述。如您所见,我删除了 SelectedValuePath,因为我不再需要 Id。但我不知道转换器要使用什么(请参阅上面的问号)。
StackOverflow 的专家们有什么想法吗?谢谢。
I'm not sure if this is possible or not. I have created a ComboBox like this...
<ComboBox Name="testType"
Margin="0,5,0,0"
IsTextSearchEnabled="True"
DisplayMemberPath="Description"
SelectedValue="{Binding MyClass.Id, Mode=TwoWay}"
SelectedValuePath="Id"/>
with code behind that loads up the available options...
DataTable testCatList = TestTypeBase.GetAll();
testType.ItemsSource = testCatList.DefaultView;
so that it properly displays all the items in MyClass. When the user makes a selection, the Id field in MyClass gets updated as intended. Great.
Here's my problem: My testCatList contains both the Id and the Description for each of the rows, and I'd like both of those fields to be bound to the current MyClass instance. So here's what I tried:
<ComboBox Name="testType"
Margin="0,5,0,0"
IsTextSearchEnabled="True"
DisplayMemberPath="Description">
<ComboBox.SelectedValue>
<MultiBinding Converter="{???}">
<Binding Path="MyClass.Id" Mode="TwoWay"/>
<Binding Path="MyClass.Description" Mode="TwoWay"/>
</MultiBinding>
</ComboBox.SelectedValue>
</ComboBox>
Here, I'd like to have MyClass.Id set to the selected Id and the MyClass.Description set to the selected Description. As you can see, I have removed the SelectedValuePath because I don't just want the Id any more. But I don't know what to use for the Converter (see the question marks above).
Any ideas, oh experts of StackOverflow? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我理解正确,请尝试使用
SelectedItem
属性而不是SelectedValue
。通过这种方式,您将获得选定的
MyClass
实例,当然还有它的所有属性。If I understood correctly try using
SelectedItem
property instead ofSelectedValue
.In that way you will get the selected
MyClass
instance and of course all of its properties.