简体   繁体   English

WinUI 3 CommunityToolkit Datagrid 在使用 CommunityToolkit.Mvvm 时显示来自两个模型的数据

[英]WinUI 3 CommunityToolkit Datagrid displaying data from two models while using CommunityToolkit.Mvvm

I have two models in my data access layer: Table1 and Table2.我的数据访问层中有两个模型:Table1 和 Table2。

I want to use the WinUI 3 DataGrid from the CommunityToolkit to display two columns from each table: Table1.ColumnA, Table1.ColumnB, Table2.ColumnC, Table2.ColumnD我想使用 CommunityToolkit 中的 WinUI 3 DataGrid 来显示每个表中的两列:Table1.ColumnA、Table1.ColumnB、Table2.ColumnC、Table2.ColumnD

My thought was to use linq in my ViewModel class to join the enumerable from each model:我的想法是在我的 ViewModel class 中使用 linq 来加入每个 model 的可枚举:

IEnumerable<Table1> table1 = unitOfWorkDbGlobal.Table1.GetAll().ToList();
IEnumerable<Table2> table2 = unitOfWorkDbGlobal.Table2.GetAll().ToList();

                var JoinedTables = (from t1 in table1
                                join t2 in table2 on t1.TestGuid equals t2.TestGuid
                                select new
                                { t1.ColumnA, t1.ColumnB, 
                                t2.ColumnC, t2.ColumnD });

The problem that occurred with this approach is that I could create a CommunityToolkit.Mvvm [ObservableProperty] with table1 or table2 as needed, but I cannot create an observable property with the join because I'm using a var type.这种方法出现的问题是,我可以根据需要使用 table1 或 table2 创建 CommunityToolkit.Mvvm [ObservableProperty] ,但我无法使用 join 创建可观察属性,因为我使用的是 var 类型。 When I use JoinedTables.GetType().Name to determine the explicit type, it returns an Enumerable<JoinIterator>d__122 4 type, which appears to be computer gobbledygook unusable as a property type.当我使用JoinedTables.GetType().Name来确定显式类型时,它返回一个Enumerable<JoinIterator>d__122 4类型,这似乎是计算机官话无法用作属性类型。

[ObservableProperty]
private ObservableCollection<Table1>? _table1Collection; //this works

[ObservableProperty]
private Enumerable<JoinIterator> d__122`4 _joinedTables; //Errors

How can the joined table be made into an ObservableProperty that can be bound in XAML to a CommunityToolkit DataGrid.如何将联接表制作成可以在 XAML 中绑定到 CommunityToolkit DataGrid 的 ObservableProperty。

Here is an example of the XAML that I would like to use (note ViewModel is assigned in the code-behind as the class with the code that I added above):这是我想使用的 XAML 的示例(注意ViewModel在代码隐藏中分配为 class 以及我在上面添加的代码):

        <controls:DataGrid x:Name="MyDataGrid"
        AutoGenerateColumns="False"
        ItemsSource="{x:Bind ViewModel.JoinedTables, Mode=OneWay}">
            <controls:DataGrid.Columns>
                <controls:DataGridTextColumn 
                Header="Column A" 
                Width="250"
                Binding="{Binding ColumnA}" 
                FontSize="14" />
                <controls:DataGridTextColumn 
                Header="Column B" 
                Width="250"
                Binding="{Binding ColumnB}" 
                FontSize="14" />
                <controls:DataGridTextColumn 
                Header="Column C" 
                Width="250"
                Binding="{Binding ColumnC}" 
                FontSize="14" />
                <controls:DataGridTextColumn 
                Header="Column D" 
                Width="250"
                Binding="{Binding ColumnD}" 
                FontSize="14" />
            </controls:DataGrid.Columns>
        </controls:DataGrid>

You need to create a type for the joined tables.您需要为连接的表创建一个类型。 Something like this.像这样的东西。

ViewModel.cs ViewModel.cs

using CommunityToolkit.Mvvm.ComponentModel;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

namespace DataGridTests;

public class Table1
{
    public int Id { get; set; }
    public string ColumnA { get; set; } = string.Empty;
    public string ColumnB { get; set; } = string.Empty;
}

public class Table2
{
    public int Id { get; set; }
    public string ColumnC { get; set; } = string.Empty;
    public string ColumnD { get; set; } = string.Empty;
}

public partial class JoinedTable : ObservableObject
{
    [ObservableProperty]
    private string columnA = string.Empty;

    partial void OnColumnAChanged(string value)
    {
        // Update database.
    }

    public string ColumnB { get; set; } = string.Empty;

    public string ColumnC { get; set; } = string.Empty;
    public string ColumnD { get; set; } = string.Empty;
}

public partial class MainWindowViewModel : ObservableObject
{
    [ObservableProperty]
    private ObservableCollection<JoinedTable> joinedTables = new();

    public MainWindowViewModel()
    {
        IEnumerable<JoinedTable> joinedTablesSource = Table1.Join(
            Table2,
            table1 => table1.Id,
            table2 => table2.Id,
            (table1, table2) => new JoinedTable()
            {
                ColumnA = table1.ColumnA,
                ColumnB = table1.ColumnB,
                ColumnC = table2.ColumnC,
                ColumnD = table2.ColumnD,
            });

        JoinedTables = new ObservableCollection<JoinedTable>(joinedTablesSource);
    }

    private ObservableCollection<Table1> Table1 { get; set; } = new()
    {
        {new Table1() { Id = 1, ColumnA="Table1-A-1", ColumnB="Table1-B-1" } },
        {new Table1() { Id = 2, ColumnA="Table1-A-2", ColumnB="Table1-B-2" } },
        {new Table1() { Id = 3, ColumnA="Table1-A-3", ColumnB="Table1-B-3" } },
    };

    private ObservableCollection<Table2> Table2 { get; set; } = new()
    {
        {new Table2() { Id = 1, ColumnC="Table2-C-1", ColumnD="Table2-D-1" } },
        {new Table2() { Id = 20, ColumnC="Table2-C-2", ColumnD="Table2-D-2" } },
        {new Table2() { Id = 3, ColumnC="Table2-C-3", ColumnD="Table2-D-3" } },
    };
}

.xaml .xaml

<toolkit:DataGrid ItemsSource="{x:Bind ViewModel.JoinedTables, Mode=OneWay}">
    <toolkit:DataGrid.Columns>
        <toolkit:DataGridTextColumn
            Binding="{Binding ColumnA, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
            Header="A"
            IsReadOnly="False" />
        <toolkit:DataGridTextColumn
            Binding="{Binding ColumnB}"
            Header="B" />
        <toolkit:DataGridTextColumn
            Binding="{Binding ColumnC}"
            Header="C" />
        <toolkit:DataGridTextColumn
            Binding="{Binding ColumnD}"
            Header="D" />
    </toolkit:DataGrid.Columns>
</toolkit:DataGrid>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 CommunityToolkit.Mvvm 调用事件 - How to call events using CommunityToolkit.Mvvm 显示 ObservableGroupedCollection 的正确方法<string, telement>使用 Wpf .NET 6 和 CommunityToolkit.Mvvm 包</string,> - Proper way of displaying an ObservableGroupedCollection<string, TElement> using Wpf .NET 6 and the CommunityToolkit.Mvvm Package 使用 CommunityToolkit.Mvvm 通知 CanExecuteChanged 的 RelayCommand 时出现 StackOverflow 异常 - StackOverflow Exception when notifying RelayCommand of CanExecuteChanged using CommunityToolkit.Mvvm 使用 CommunityToolkit.Mvvm 在 ObservableProperty 更改时调用方法 - Call method when ObservableProperty changes using CommunityToolkit.Mvvm C# CommunityToolkit.Mvvm ObservableProperty 列表 - C# CommunityToolkit.Mvvm ObservableProperty on a list 使用 CommunityToolkit.Mvvm 处理可观察对象属性 - Handling observable object properties with CommunityToolkit.Mvvm WinUI DataGrid(来自 CommunityToolkit):如何获取当前选定的单元格? - WinUI DataGrid (from CommunityToolkit): How to get the currently selected cell? 无法在使用 CommunityToolkit.Mvvm 的视图模型中使用 ICommand 属性 - Can't use ICommand attribute in view model using CommunityToolkit.Mvvm 如何在 C# 中使用 Community - How do I bind an object that is periodically modified to a treeview in C# WPF using the CommunityToolkit.MVVM? 如何将 CommunityToolkit.Mvvm 中的源代码生成器用于 .NET 框架 4.7.2 WPF 应用程序 - How to use the source generators from CommunityToolkit.Mvvm for a .NET Framework 4.7.2 WPF Application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM