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

╄→гoц情女王★ 提交于 2019-12-24 13:42:47

问题


I am developing a simple WPF application that talks to an Access 2000 database. I have used entity framework in the past, but it seems I am limited with an Access database. I managed to generate a DataSet xsd file, that contains mappings for each table and the relations between tables. 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. I use the following C# to achieve this in the WPF code behind:

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. I am able to retrieve the basic details from within the Transaction table. 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:

<!--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?


回答1:


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

    }



回答2:


Your table have Dentist name and you are binding with Dentists

Anyway, try this;

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

And;

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


来源:https://stackoverflow.com/questions/19282589/wpf-xaml-bind-datagrid-column-to-foreign-key-dataset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!