簡體   English   中英

如何從Observablecollection設置組合框的ItemsSource

[英]How to set ItemsSource of a combobox from a Observablecollection

嗨,我正在使用MVVM-Light框架從事WinRT項目。 我有一個列表視圖,其中ItemsSource是ViewModel上的ObservableCollection。 此ObservableCollection中的對象(ClassOne)具有一個字段,該字段本身就是ObservableCollection。 在Listview中,我有ComboBoxes,誰的itemSource我想綁定到第二個ObservableCollection(這是另一個observablecollection中的字段)。 第二個ObservableCollection動態填充到我的View的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}}">    

但這是行不通的。

這是我在ViewModel上的CollectionOne的屬性:

private ObservableCollection<ClassOne> _collectionOne;
    public ObservableCollection<ClassOne> CollectionOne
    {
        get { return _collectionOne;; }
        set
        {
            if (_collectionOne; == value)
            {
                return;
            }
            _collectionOne; = value;
            RaisePropertyChanged(() => CollectionOne);
        }
    }

這是ObservableCollection(ClassOne)內部的類:

public class ClassOne
{
    public string DataCollectionOne{ get; set; }
    public ObservableCollection<ClassTwo> CollectionTwo{ get; set; }
}

第二類只是包含一個String屬性。

public class ClassTwo
{
    public string DataCollectionTwo{ get; set; }
}

有任何想法嗎?

我在一個測試項目中嘗試過

您可以簡單地交換您的代碼

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

<ComboBox ItemsSource="{Binding CollectionTwo}">

在我的測試項目中效果很好。

希望有幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM