绑定 Collection-ListBox-DataGrid [英] Binding Collection-ListBox-DataGrid
问题描述
我有两个数据库表:Account 和 AccountRecords.它们与外键连接,因为每个帐户都包含多个记录.我使用 ObservableCollection 将 ListBox 与 Accounts 绑定:
i have two Database Tables: Account and AccountRecords. They are connected with a Foreign Key because every Account contains multiple Records. I use ObservableCollection to bind the ListBox with the Accounts:
<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 与 ListBox 中的选定项绑定:
Then I bind the DataGrid with the selected Item in the ListBox:
<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>
一切正常.我的问题是如何在我的 DataGrid 中显示每个帐户的记录?记录位于单独的表中.如果我创建第二个 Observable 集合,如何在 DataGrid 中显示记录和帐户?
This is everything OK. My question is how can i show in my DataGrid the Records for each Account? The Records are in a separate Table. If i create a second Observable Collection how can show the Records and the Accounts in the DataGrid?
谢谢.乔治
推荐答案
您可以使用 RowDetails 模板.示例代码如下:
You can use RowDetails Template. Example Code is Below:
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; }
}
}
这篇关于绑定 Collection-ListBox-DataGrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!