简体   繁体   中英

DataGrid ItemSource bound to selected item within another DataGrid

I have the following Data Structures:

List<Customer> currentCustomers {...}

public class Customer 
{
    public string ID { get, set }
    public string Name { get, set } 
    [...]
    public List<Products> { get, set } 
}

I have a customers DataGrid bound to the currentCustomers List. What I would like to be able to do is bind a second DataGrid to the selected item within customers to display all the Product information for that Customer.

ie The user clicks on a Customer within the Customers DataGrid, this then automatically updates a second DataGrid based on that Customers Products.

Is this even possible?

If so, is there a resource around that will tell/show me how this is done?

This should work:

<DataGrid x:Name="one"></DataGrid>
<DataGrid x:Name="two" DataContext="{Binding ElementName=one, Path=SelectedItem.Products}"></DataGrid>

Just bind it to SelectedItem property:

<DataGrid x:Name="customersList" CanSelectMultipleItems="false" ... />

<DataGrid x:Name="customerDetails" 
          ItemsSource = "{Binding ElementName = customersList, 
                                  Path = SelectedItem.Products}">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM