【发布时间】:2021-04-07 14:54:58
【问题描述】:
解释一下我的 WPF 应用程序的背景:
在我的数据库中,我有三个表:文档、文档类型、元数据
每个文档都有一个文档类型。 Doctype 具有用户输入的元数据。
例如:
我有两种文档类型:Invoice 和 Mail。
如果用户添加了一个文档,他需要选择其中一种文档类型。
如果用户选择Invoice,则要求他输入InvoiceNumber 和Amount 作为元数据。
如果用户选择Mail,则会要求他提供Sender 和Subject 作为元数据。
在 WPF 应用程序中,我有一个数据网格,应该显示所有文档和相关数据。对于每个元数据,应自动创建一列。
对于我的示例,这意味着我想要创建一个包含以下列的数据网格:
docname|doctype|InvoiceNumber|Amount|Sender|Subject
此值例如:
doc1|发票|IN1234|10.00| |
doc2|邮件| | |汤姆|测试邮件
由于缺少文档类型的元数据而导致的空值应该在数据网格中留空。
我有以下代码来创建示例数据并将其绑定到我的数据网格:
Class MainWindow
Private Sub MainWindow_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
Dim lst As New ObservableCollection(Of doc)
'Example doctypes
Dim DoctypeExampleInvoice As New Dictionary(Of String, String)
DoctypeExampleInvoice.Add("InvoiceNumber", "IN1234")
DoctypeExampleInvoice.Add("InvoiceAmount", "10.00")
Dim DoctypeExampleMail As New Dictionary(Of String, String)
DoctypeExampleMail.Add("Sender", "Tom")
DoctypeExampleMail.Add("Subject", "Testmail")
'Example docs
Dim _doc As New doc With {.docname = "doc1", .doctype = "Invoice", .metadata = DoctypeExampleInvoice}
lst.Add(_doc)
Dim _doc2 As New doc With {.docname = "doc2", .doctype = "Mail", .metadata = DoctypeExampleMail}
lst.Add(_doc2)
dgv.ItemsSource = lst
End Sub
End Class
Public Class doc
Property docname As String
Property doctype As String
Property metadata As New Dictionary(Of String, String)
End Class
我的 XAML 如下所示:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<DataGrid x:Name="dgv" ItemsSource="{Binding lst}" SelectionMode="Single" SelectionUnit="FullRow" AutoGenerateColumns="True"/>
</Grid>
</Window>
如您所见,元数据未显示为列,而是显示为包含集合的一列。如何完成这项工作?
提前致谢, 马可
【问题讨论】:
-
在您的
doc类中,您应该创建映射到您的metadata列表的key1、key2和key3属性。 -
嗨,艾蒂安,你是对的。我需要问不同的。我将重写我的问题以更好地解释我的问题。