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>