WPF XAML - 将datagrid列绑定到外键(Dataset)

时间:2013-10-09 20:57:58

标签: c# wpf xaml ms-access datagrid

我正在开发一个与Access 2000数据库通信的简单WPF应用程序。我过去使用过实体框架,但似乎我受限于Access数据库。我设法生成一个DataSet xsd文件,其中包含每个表的映射以及表之间的关系。架构如下所示:

db schema

要将数据绑定到WPF数据网格,我将DataContext设置为DataSet,然后从各种表适配器填充它。我在后面的WPF代码中使用以下C#来实现这一点:

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

我想显示一个WPF数据网格,其中包含“事务表”中的详细信息。我能够从Transaction表中检索基本细节。我想要实现的是能够导航表具有的关系,以便我可以显示牙医名称,而不是外键值。我尝试过以下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>

注意牙医专栏下的绑定。当我尝试这个时,我得到一个空数据列。如何使用XAML导航关系?

2 个答案:

答案 0 :(得分:0)

enter image description here以下是工作示例的完整示例。请编辑此内容,以便我们查看问题所在。

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

    }

答案 1 :(得分:0)

您的桌子上有 Dentist 名称,并且您与牙医 s

绑定

无论如何,试试这个;

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

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