C# 如何从Observablecollection设置组合框的ItemsSource

C# 如何从Observablecollection设置组合框的ItemsSource,c#,combobox,mvvm-light,winrt-xaml,observablecollection,C#,Combobox,Mvvm Light,Winrt Xaml,Observablecollection,嗨,我正在从事一个使用MVVM Light框架的WinRT项目。 我有一个listview,其中ItemsSource是我的ViewModel上的一个可见集合。 此ObservableCollection中的对象(ClassOne)有一个字段,该字段本身就是ObservableCollection。 在Listview中,我有一些组合框,它们是我想要绑定到第二个ObservableCollection的itemsSource(这是另一个ObservableCollection中的字段)。 第二个

嗨,我正在从事一个使用MVVM Light框架的WinRT项目。 我有一个listview,其中ItemsSource是我的ViewModel上的一个可见集合。 此ObservableCollection中的对象(ClassOne)有一个字段,该字段本身就是ObservableCollection。 在Listview中,我有一些组合框,它们是我想要绑定到第二个ObservableCollection的itemsSource(这是另一个ObservableCollection中的字段)。 第二个ObservableCollection在我的视图的ViewModel中动态填充

我的Xaml代码:

<ListView ItemsSource="{Binding CollectionOne}">
  <ListView.ItemTemplate>
    <DataTemplate>
 <StackPanel>
     <TextBlock Text="{Binding DataCollectionOne}"></TextBlock>                        
     <ComboBox ItemsSource="{Binding Path=CorrespondingViewModel.CollectionOne.CollectionTwo, Source={StaticResource Locator}}">
        <ComboBox.ItemTemplate>
           <DataTemplate>
              <TextBlock Text="{Binding DataCollectionTwo}"/>
           </DataTemplate>
        </ComboBox.ItemTemplate>
      </ComboBox> 
</StackPanel>
    </DataTemplate>
  </ListView.ItemTemplate>
</ListView>

有什么想法吗?

我在一个测试项目中尝试过

您可以简单地交换代码

<ComboBox ItemsSource="{Binding Path=CorrespondingViewModel.CollectionOne.CollectionTwo, Source={StaticResource Locator}}"> 


在我的测试项目中,效果很好


希望有帮助

我在一个测试项目中尝试过

您可以简单地交换代码

<ComboBox ItemsSource="{Binding Path=CorrespondingViewModel.CollectionOne.CollectionTwo, Source={StaticResource Locator}}"> 


在我的测试项目中,效果很好


希望有帮助

谢谢你的建议,但我需要的领域是在CollectionOne中的inside Collection2。所以正常的绑定似乎不起作用。我完全用你的代码做了测试项目,并交换了我上面发布的内容:绑定对我有效,这个属性是否在Collection2中?谢谢,我第一次在viewmodel中出错时,你的方法确实有效。谢谢你的建议,但我需要的领域是在Collection2里面谁在CollectionOne里面。所以正常的绑定似乎不起作用。我完全用你的代码做了测试项目,并交换了我上面发布的内容:绑定对我有效,这个属性是否在Collection2中?谢谢,我第一次在viewmodel中出错时,你的方法确实有效。
public class ClassOne
{
    public string DataCollectionOne{ get; set; }
    public ObservableCollection<ClassTwo> CollectionTwo{ get; set; }
}
public class ClassTwo
{
    public string DataCollectionTwo{ get; set; }
}
<ComboBox ItemsSource="{Binding Path=CorrespondingViewModel.CollectionOne.CollectionTwo, Source={StaticResource Locator}}"> 
<ComboBox ItemsSource="{Binding CollectionTwo}">