【问题标题】:How to merge two different observable collection in DataGrid如何在 DataGrid 中合并两个不同的可观察集合
【发布时间】:2016-09-06 12:28:32
【问题描述】:

我想组合下面的集合并使用 CompositeCollection 将其绑定到 DataGrid。

   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 为空。

【问题讨论】:

标签: wpf datagrid


【解决方案1】:

谢谢你们。

我已经达到了我的要求,如下所示,

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-07
    • 1970-01-01
    • 2020-04-03
    相关资源
    最近更新 更多