0

I want to combine the below collection and bind it to DataGrid by using CompositeCollection.

   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"/>

But it shown as empty grid. How to merge Label and NumericLabelDataSource in wpf? How to display combosite collection in DataGrid control. DataGrid is empty when try to binding the CompositeCollection.

Smirti
  • 305
  • 2
  • 12
  • http://stackoverflow.com/questions/6446699/how-do-you-bind-a-collectioncontainer-to-a-collection-in-a-view-model – lerner1225 Sep 06 '16 at 12:40
  • Another example: http://stackoverflow.com/q/19243109/109702 Try defining the CompositeCollection and it's CollectionContainer items declaratively rather than in code. – slugster Sep 06 '16 at 12:43
  • If I'm not wrong on guessing your requirement, you won't get the expected result of what you want with `CompositeCollection` over `DataGrid`. :) – Gopichandar Sep 06 '16 at 12:49

1 Answers1

0

Thank you guys.

I have achieved my requirement like below,

<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>
Smirti
  • 305
  • 2
  • 12