2013-05-20 92 views
0

我有兩個數據庫表:Account和AccountRecords。他們與外鍵連接,因爲每個帳戶都包含多個記錄。我使用的ObservableCollection與賬戶列表框綁定:綁定收藏列表框-DataGrid

<ListBox Name="ListAccount" 
     ItemsSource="{Binding CurrentHouse.Account}"> 
    <ListBox.ItemTemplate> 
    <DataTemplate> 
     <Grid> 
     <TextBlock> 
      <TextBlock.Text> 
      <MultiBinding StringFormat="{}{0} {1}"> 
       <Binding Path="AccountNumber" /> 
       <Binding Path="Name" /> 
      </MultiBinding> 
      </TextBlock.Text> 
     </TextBlock> 
     </Grid> 
    </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

然後我綁定的DataGrid在列表框中選擇的項目:

<DataGrid ItemsSource="{Binding ElementName=ListAccount, Path=SelectedItems}"> 
    <DataGrid.Columns> 
    <DataGridTextColumn Binding="{AccountNumber}" 
         Header="Nr" 
         FontSize="16" /> 
    <DataGridTextColumn Binding="{Name}" 
         Header="Name" 
         FontSize="16" /> 
    </DataGrid.Columns> 

這一切就OK了。我的問題是我如何在DataGrid中顯示每個賬戶的記錄?記錄在一個單獨的表格中。如果我創建第二個Observable集合,那麼如何在DataGrid中顯示記錄和帳戶?

謝謝。 Georg

+0

你有沒有看過WPF DataGrid的RowDetailsTemplate? – cvraman

回答

1

您可以使用RowDetails模板。示例代碼如下:

XAML:

<Window x:Class="TestWPFApp.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:TestWPFApp" 
    Title="MainWindow" Height="350" Width="525"> 

<Grid> 
    <DataGrid ItemsSource="{Binding AccountList}" AutoGenerateColumns="False"> 
     <DataGrid.Columns> 
      <DataGridTextColumn Binding="{Binding AccountNumber}" Header="Account Number" FontSize="16"/> 
      <DataGridTextColumn Binding="{Binding Name}" Header="Name" FontSize="16"/> 
     </DataGrid.Columns> 
     <DataGrid.RowDetailsTemplate> 
      <DataTemplate> 
       <DataGrid ItemsSource="{Binding RecordList,Mode=TwoWay}" AutoGenerateColumns="False"> 
        <DataGrid.Columns> 
         <DataGridTextColumn Binding="{Binding RecordNumber}" Header="Record Number" FontSize="16"/> 
         <DataGridTextColumn Binding="{Binding Name}" Header="Name" FontSize="16"/> 
        </DataGrid.Columns> 
       </DataGrid> 
      </DataTemplate> 
     </DataGrid.RowDetailsTemplate> 
    </DataGrid> 
</Grid> 
</Window> 

代碼隱藏:

using System.Collections.ObjectModel; 
using System.Windows; 

namespace TestWPFApp 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
      this.DataContext = new ViewModel(); 
     } 
    } 


    public class ViewModel 
    { 
     public ViewModel() 
     { 
      //Sample Data 
      var recordList = new ObservableCollection<Record>(); 
      recordList.Add(new Record() { RecordNumber = "R1", Name = "R-Name-1" }); 
      recordList.Add(new Record() { RecordNumber = "R2", Name = "R-Name-2" }); 
      recordList.Add(new Record() { RecordNumber = "R3", Name = "R-Name-3" }); 
      recordList.Add(new Record() { RecordNumber = "R4", Name = "R-Name-4" }); 

      AccountList = new ObservableCollection<Account>(); 
      AccountList.Add(new Account() { AccountNumber = "A1111", Name = "A-Name-1", RecordList = recordList }); 
      AccountList.Add(new Account() { AccountNumber = "A2222", Name = "A-Name-2", RecordList = recordList }); 
      AccountList.Add(new Account() { AccountNumber = "A3333", Name = "A-Name-3", RecordList = recordList }); 
      AccountList.Add(new Account() { AccountNumber = "A4444", Name = "A-Name-4", RecordList = recordList }); 
     } 
     public ObservableCollection<Account> AccountList { get; set; } 
    } 

    public class Account 
    { 
     public string AccountNumber { get; set; } 
     public string Name { get; set; } 
     public ObservableCollection<Record> RecordList { get; set; } 
    } 

    public class Record 
    { 
     public string RecordNumber { get; set; } 
     public string Name { get; set; } 
    } 
} 
+0

我試過你的代碼,它的工作原理。唯一的問題是,我必須單擊DataGrid.Columns(例如在AccountNumber上)才能使RecordList可見。是否有機會使其默認可見? – Georg

+0

使所有帳戶都可見? – cvraman

+0

我只看到AccountNumber和Name,但沒有記錄。我必須點擊一個有AccountNumber的行才能看到裏面的記錄。 – Georg

1
public class Account 
{ 
    public int AccountNumber { get; set; } 
    public string Name { get; set; } 
} 

public class AccountRecords 
{ 
    public int AccountNumber { get; set; } 
    public int AccountRecordNumber { get; set; } 
    public string Details { get; set; } 
} 

public class AccountManager 
{ 
    public static List<Account> GetAccounts() 
    { 
     List<Account> accounts = new List<Account>() 
      { 
       new Account() { AccountNumber = 1, Name = "Account 1"}, 
       new Account() { AccountNumber = 2, Name = "Account 2"} 
      }; 
     return accounts; 
    } 

    public static List<AccountRecords> GetAccountRecords(int accountNumber) 
    { 
     List<AccountRecords> records = new List<AccountRecords>() 
      { 
       new AccountRecords() {AccountNumber = 1, AccountRecordNumber = 1, Details = "1.1 record"}, 
       new AccountRecords() {AccountNumber = 1, AccountRecordNumber = 2, Details = "1.2 record"}, 
       new AccountRecords() {AccountNumber = 2, AccountRecordNumber = 3, Details = "2.2 record"}, 
       new AccountRecords() {AccountNumber = 2, AccountRecordNumber = 4, Details = "2.4 record"}, 
       new AccountRecords() {AccountNumber = 2, AccountRecordNumber = 5, Details = "2.5 record"}, 
      }; 
     return records.Where(q => q.AccountNumber == accountNumber).ToList(); 
    } 
} 

public class BindingRelationalDataViewModel : INotifyPropertyChanged 
{ 
    public List<Account> Accounts { get; set; } 

    private List<AccountRecords> currentAccountRecords; 
    public List<AccountRecords> CurrentAccountRecords 
    { 
     get { return currentAccountRecords; } 
     set 
     { 
      currentAccountRecords = value; 
      RaisePropertyChanged("CurrentAccountRecords"); 
     } 
    } 

    public BindingRelationalDataViewModel() 
    { 
     Accounts = AccountManager.GetAccounts(); 
     CollectionViewSource.GetDefaultView(Accounts).CurrentChanged += new EventHandler(BindingRelationalDataViewModel_CurrentChanged); 
    } 

    void BindingRelationalDataViewModel_CurrentChanged(object sender, EventArgs e) 
    { 
     int accountNumber = ((Account)CollectionViewSource.GetDefaultView(Accounts).CurrentItem).AccountNumber; 
     CurrentAccountRecords = AccountManager.GetAccountRecords(accountNumber); 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    private void RaisePropertyChanged(string propertyName) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 
} 

XAML

<Window x:Class="StackOverFlowQuestions.BindingRelationalData" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:StackOverFlowQuestions" 
    Title="BindingRelationalData" Height="300" Width="300"> 
<Window.Resources> 
    <local:BindingRelationalDataViewModel x:Key="BindingRelationalDataViewModel"></local:BindingRelationalDataViewModel> 
</Window.Resources> 
<StackPanel DataContext="{Binding Source={StaticResource ResourceKey=BindingRelationalDataViewModel}}"> 
    <ListBox Name="ListAccount" ItemsSource="{Binding Path=Accounts}" IsSynchronizedWithCurrentItem="True"> 
     <ListBox.ItemTemplate> 
      <DataTemplate > 
       <Grid> 
        <TextBlock> 
          <TextBlock.Text> 
           <MultiBinding StringFormat="{}{0} {1}" > 
            <Binding Path="AccountNumber" /> 
            <Binding Path="Name" /> 
           </MultiBinding> 
          </TextBlock.Text> 
        </TextBlock> 
       </Grid> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 

    <DataGrid ItemsSource="{Binding Path=CurrentAccountRecords}" AutoGenerateColumns="False"> 
     <DataGrid.Columns> 
      <DataGridTextColumn Binding="{Binding AccountRecordNumber}" Header="Nr" FontSize="16"/> 
      <DataGridTextColumn Binding="{Binding Details}" Header="Name" FontSize="16"/> 
     </DataGrid.Columns> 
    </DataGrid> 
</StackPanel>