一个 Itemssource MVVM 中的多个数据网格视图

本文关键字:数据 数据网 网格 视图 Itemssource MVVM 一个 | 更新日期: 2023-09-27 18:34:33

我有很多数据网格视图(自动创建控件(使用项目控制((

和来自ICollection数据的One Items来源(数据(;

如何使用条件为每个数据网格视图绑定项目源

前任:

datagridview1: itemsource=datas WHERE datas.detailsID=1

datagridview2: itemsource=datas WHERE datas.detailsID=2

在 XAML 中(自动控制(

<ScrollViewer HorizontalScrollBarVisibility="Visible">
    <ItemsControl ItemsSource="{Binding datas}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <WrapPanel>
                    <DataGrid ItemsSource="{Binding data}" />
                </WrapPanel>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    </ItemsControl>
</ScrollViewer>

在视图中模型

数据;...

一个 Itemssource MVVM 中的多个数据网格视图

为什么要让自己为难?这是 WPF 和 MVVM,因此您只需将原始集合拆分为所需的任意多个数据源即可。只需为每个ItemsSource定义一个集合属性,并在访问原始数据集合的数据时填充它们。然后,您只需将数据绑定每个属性到其相关的ItemSource属性即可。


更新>>>

假设这是您的原始收藏:

private ObservableCollection<YourDataType> collection = new ObservableCollection<YourDataType>();
public ObservableCollection<YourDataType> Collection
{
    get { return collection; }
    set { collection = value; NotifyPropertyChanged("Collection"); }
}

创建更多集合属性 - 根据需要创建尽可能多的属性:

private ObservableCollection<YourDataType> otherCollection = new ObservableCollection<YourDataType>();
public ObservableCollection<YourDataType> OtherCollection
{
    get { return otherCollection; }
    set { otherCollection = value; NotifyPropertyChanged("OtherCollection"); }
}

以所需的任何方式从原始集合中填充它们:

OtherCollection = Collection.Where(d => d.DetailsId == 1);

现在,只需将数据将新集合属性绑定到各种DataGrid

<DataGrid ItemsSource="{Binding OtherCollection}" />
...
<DataGrid ItemsSource="{Binding AnotherCollection}" />