如何通过与DataGrid的ItemsSource不同的ItemsSource填充DataGrid ComboBoxColumn [英] How do I fill a DataGrid ComboBoxColumn by an ItemsSource that is different from the DataGrid's ItemsSource

查看:89
本文介绍了如何通过与DataGrid的ItemsSource不同的ItemsSource填充DataGrid ComboBoxColumn的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好亲爱的读者,



我有以下问题:



我有一个观点,在那个视图上我放置了一个DataGrid。我通过Binding将DataGrid的ItemsSource连接到ViewModel中的Property。它工作正常。

有一个我想要显示为ComboBoxColumn的列,因为用户应该有一定数量的数据放在这个列中。问题是,如何将ItemsSource设置为不属于我将DataGrid连接到的ItemsSource的List?



这里使用了一些代码:

 <   DataGrid     AutoGenerateColumns   =     ItemsSource   =  {Binding Bikes} >  
< DataGrid.Columns >
< DataGridTextColumn 标题 = 制造商 绑定 = {Binding Manufacturer} >
< / DataGridTextColumn >

< DataGridComboBoxColumn 标题 = 选择 SelectedItemBinding = {Binding Owner}

< span class =code-attribute> ItemsSource = {Binding Persons} >
< ; / DataGridComboBoxColumn >
< / DataGrid.Columns >
< / DataGrid >





我想我知道这个解决方案不起作用,因为sinc将DataGrid的ItemsSource设置为Bikes,ComboBoxColumn的ItemsSource查看Bikes中的对象并搜索Property Persons。

但是Persons是另一个在ViewModel中创建的List。 />


我真的无法弄清楚如何以正确的方式设置ComboBox的数据绑定。



我希望任何人都可以帮助。如果您需要更多信息,请告诉我。



每个人都有一个好人。

解决方案

之后另一个小时的研究我尝试了这个 http://stackoverflow.com/a/5409984 [ ^ ]



和它工作:)

Hello dear readers,

I have the following problem:

I have a view and on that view I placed a DataGrid. I connected the ItemsSource of the DataGrid via Binding to a Property in the ViewModel. It works fine.
There is one Column that I want to show as ComboBoxColumn because the User shall have an amount of data to put in this column. The question is, how can I set the ItemsSource to a List that is not part of the ItemsSource I connected the DataGrid to?

Here some code i used:

<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Bikes}">
   <DataGrid.Columns>
      <DataGridTextColumn Header="Manufacturer" Binding="{Binding Manufacturer}">
      </DataGridTextColumn>
               
      <DataGridComboBoxColumn Header="Choice" SelectedItemBinding="{Binding Owner}"

         ItemsSource="{Binding Persons}">
      </DataGridComboBoxColumn>
   </DataGrid.Columns>
</DataGrid>



I think I know that this solution is not working because since the ItemsSource of the DataGrid is set to Bikes the ItemsSource of the ComboBoxColumn looks into the objects in Bikes and searches for a Property Persons.
But Persons is another List that is created in the ViewModel.

I really cannot figure out how to set the databinding of the ComboBox in the correct way.

I hope that anyone can help with that. If you need further information please let me know.

Have a good one everybody.

解决方案

After another hour of research I tried the code from this http://stackoverflow.com/a/5409984[^]

AND IT WORKED :).


这篇关于如何通过与DataGrid的ItemsSource不同的ItemsSource填充DataGrid ComboBoxColumn的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆