简体   繁体   English

WPF XAML-将datagrid列绑定到外键(数据集)

[英]WPF XAML - Bind datagrid column to foreign key (Dataset)

I am developing a simple WPF application that talks to an Access 2000 database. 我正在开发一个与Access 2000数据库对话的简单WPF应用程序。 I have used entity framework in the past, but it seems I am limited with an Access database. 我过去使用过实体框架,但似乎我对Access数据库有一定的限制。 I managed to generate a DataSet xsd file, that contains mappings for each table and the relations between tables. 我设法生成一个DataSet xsd文件,其中包含每个表的映射以及表之间的关系。 The schema is shown below: 该架构如下所示:

数据库模式

To bind the data to a WPF datagrid, I set the DataContext to a DataSet, after filling it from various table adapters. 为了将数据绑定到WPF数据网格,在通过各种表适配器填充数据后,我将DataContext设置为DataSet。 I use the following C# to achieve this in the WPF code behind: 我使用以下C#在后面的WPF代码中实现此目的:

public partial class MainWindow : Window
{   
    private TillDataSet ds = new TillDataSet();
    private TransactionTableAdapter transactionDa = new TransactionTableAdapter();
    private DentistTableAdapter dentistDa = new DentistTableAdapter();
    private Transaction_TypeTableAdapter transactionTypeDa = new Transaction_TypeTableAdapter();
    private Payment_MethodTableAdapter paymentMethodDa = new Payment_MethodTableAdapter();

    public MainWindow()
    {
        InitializeComponent();
    }

    private void fillDataAdapters()
    {
        transactionDa.Fill(ds.Transaction);
        dentistDa.Fill(ds.Dentist);
        transactionTypeDa.Fill(ds.Transaction_Type);
        paymentMethodDa.Fill(ds.Payment_Method);
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        fillDataAdapters();
        this.DataContext = ds;
    }
}

I want to display a WPF datagrid, that contains details from the Transaction Table. 我想显示一个WPF数据网格,其中包含来自事务表的详细信息。 I am able to retrieve the basic details from within the Transaction table. 我能够从Transaction表中检索基本详细信息。 What I want to achieve is to be able to navigate the relationships the table has, such that I can display the Dentists name, instead of the foreign key value. 我要实现的是能够导航表具有的关系,这样我就可以显示牙医的姓名,而不是外键值。 I have tried the following XAML: 我尝试了以下XAML:

<!--Transaction List-->
<DataGrid Grid.Row="1" Grid.Column="1" Name="dtgTransactions" AutoGenerateColumns="False" IsReadOnly="True" Margin="10" ItemsSource="{Binding Transaction}">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Date" Binding="{Binding Trans_Date}" />
        <DataGridTextColumn Header="Type" Binding="{Binding Type}" />
        <DataGridTextColumn Header="Dentist" Binding="{Binding Dentist.Dentist_Name}"/>
        <DataGridTextColumn Header="Payment Method" Binding="{Binding Method}" />
    </DataGrid.Columns>
</DataGrid>

Notice the binding under the Dentist column. 请注意“牙医”列下的绑定。 When I try this, I get an empty data column. 尝试此操作时,我得到一个空的数据列。 How can I navigate the relationship using XAML? 如何使用XAML导航关系?

在此处输入图片说明 Here is a full example of working example. 这是工作示例的完整示例。 Please edit this so we can see where the issue is. 请对此进行编辑,以便我们查看问题出在哪里。

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel Orientation="Vertical" Loaded="StackPanel_Loaded" >
        <DataGrid Grid.Row="1" Grid.Column="1" Name="dtgTransactions" AutoGenerateColumns="False" IsReadOnly="True" Margin="10" ItemsSource="{Binding Transaction}">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Date" Binding="{Binding Trans_Date}" />
                <DataGridTextColumn Header="Type" Binding="{Binding Type}" />
                <DataGridTemplateColumn Header="Dentist">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ItemsControl ItemsSource="{Binding Dentists}">
                                <ItemsControl.ItemTemplate>
                                    <DataTemplate>
                                        <StackPanel Orientation="Horizontal">
                                            <TextBlock Text="{Binding Dentist_Name}"/>
                                        </StackPanel>
                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>
                            </ItemsControl>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
    </StackPanel>
</Window>
  using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; namespace WpfApplication1 { public class ViewModel { public Transactions Transaction { get; set; } public ViewModel() { Transaction = new Transactions(); Transaction transData = new Transaction(); transData.ID = 0; transData.Trans_Date = DateTime.Now; transData.Type = "Type1"; Dentist dentistData = new Dentist(); dentistData.ID = 0; dentistData.Dentist_Name = "Dentist1 Name"; transData.Dentists.Add(dentistData); Transaction.Add(transData); transData = new Transaction(); transData.ID = 1; transData.Trans_Date = DateTime.Now; transData.Type = "Type2"; dentistData = new Dentist(); dentistData.ID = 1; dentistData.Dentist_Name = "Dentist2 Name"; transData.Dentists.Add(dentistData); dentistData = new Dentist(); dentistData.ID = 2; dentistData.Dentist_Name = "Dentist3 Name"; transData.Dentists.Add(dentistData); Transaction.Add(transData); } } public class Transactions : ObservableCollection<Transaction> { public ObservableCollection<Transaction> TransactionList { get; set; } public Transactions() { TransactionList = new ObservableCollection<Transaction>(); } } public class Transaction { public ObservableCollection<Dentist> Dentists { get; set; } public int ID { get; set; } public DateTime Trans_Date { get; set; } public string Type { get; set; } public Transaction() { Dentists = new ObservableCollection<Dentist>(); } } public class Dentist { public int ID { get; set; } public string Dentist_Name { get; set; } } } 

Your table have Dentist name and you are binding with Dentist s 你的表有牙医的名字,你与牙医S结合

Anyway, try this; 无论如何,尝试一下;

<ItemsControl ItemsSource="{Binding Path=DataContext.Dentist,
 RelativeSource={RelativeSource AncestorType={x:Type Window}}}">

And; 和;

<TextBlock Text="{Binding Dentist_Name}"/>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM