2016-09-06 17 views
0

以下のコレクションを結合し、CompositeCollectionを使用してDataGridにバインドします。DataGridで2つの異なるobservableコレクションをマージする方法

private ObservableCollection<LabelDataInfo> labelSource; 

    public ObservableCollection<LabelDataInfo> LabelSource 
    { 
     get { return labelSource; } 
     set { labelSource = value; } 
    } 
    private ObservableCollection<NumericalDataInfo> numericLabelSource; 

    public ObservableCollection<NumericalDataInfo> NumericLabelSource 
    { 
     get { return numericLabelSource; } 
     set { numericLabelSource = value; } 
    } 
    private CompositeCollection mergedSource; 

    public CompositeCollection MergedSource 
    { 
     get { return mergedSource; } 
     set { mergedSource = value; OnPropertyChanged("MergedSource"); } 
    } 

public MergedTargetDataInfo() 
    { 
     var labelInfo = new ObservableCollection<LabelDataInfo>(); 
     labelInfo.Add(new LabelDataInfo() { Title = "Title1", Label = "Label1" }); 
     labelInfo.Add(new LabelDataInfo() { Title = "Title2", Label = "Label2" }); 
     labelInfo.Add(new LabelDataInfo() { Title = "Title3", Label = "Label3" }); 

     LabelSource = labelInfo; 

     var numericInfo = new ObservableCollection<NumericalDataInfo>(); 
     numericInfo.Add(new NumericalDataInfo() { NumericTitle = "NumericTitle1", NumericLabel = "NLabel1" }); 
     numericInfo.Add(new NumericalDataInfo() { NumericTitle = "NumericTitle2", NumericLabel = "NLabel2" }); 
     numericInfo.Add(new NumericalDataInfo() { NumericTitle = "NumericTitle3", NumericLabel = "NLabel3" }); 
     NumericLabelSource = numericInfo; 


     mergedSource = new CompositeCollection(); 
     CollectionContainer collection1 = new CollectionContainer() { Collection = LabelSource }; 
     CollectionContainer collection2 = new CollectionContainer() { Collection = NumericLabelSource };   
     mergedSource.Add(collection1); 
     mergedSource.Add(collection2);   
    } 



<DataGrid ItemsSource="{Binding MergedSource}" AutoGenerateColumns="True"/> 

しかし、それは空のグリッドとして表示されます。 wpfでLabelとNumericLabelDataSourceをマージするには? DataGridコントロールにコンボサイトコレクションを表示する方法。 CompositeCollectionをバインドしようとすると、DataGridは空です。

+0

http://stackoverflow.com/questions/6446699/how-do-you-bind-a-collectioncontainer-to-a-collection-in- a-view-model – lerner1225

+0

別の例:http://stackoverflow.com/q/19243109/109702コードではなく、宣言的にCompositeCollectionとそのCollectionContainerアイテムを定義してみてください。 – slugster

+0

あなたの要件を推測するのが間違っていなければ、 'DataGrid'を介して' CompositeCollection'で期待した結果を得ることはできません。 :) – Gopichandar

答えて

0

ありがとうございます。

私は以下のように私の要件を達成している、

<DataGrid AutoGenerateColumns="False"> 
     <DataGrid.ItemsSource> 
      <CompositeCollection> 
       <CollectionContainer Collection="{Binding Source={StaticResource viewModel}, Path=LabelSource}"/> 
       <CollectionContainer Collection="{Binding Source={StaticResource viewModel},Path=NumericLabelSource}"/> 
      </CompositeCollection> 
     </DataGrid.ItemsSource> 
     <DataGrid.Columns> 
      <DataGridTextColumn Binding="{Binding Title}"/> 
      <DataGridTextColumn Binding="{Binding Label}"/> 
     </DataGrid.Columns> 
    </DataGrid> 
関連する問題