好的,如果我理解正确,您有一个 List List<Customer>
,列表 List 绑定到 ComboBox,每个子列表绑定到 DataGrid
例子:
xml:
<Window x:Class="WpfApplication13.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" Name="UI">
<Grid DataContext="{Binding ElementName=UI}">
<ComboBox DataContext="{Binding ComboItems}" Height="27" VerticalAlignment="Top" >
<DataGrid ItemsSource="{Binding}" AutoGenerateColumns="False" ColumnWidth="150" >
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding CustomerName}" />
<DataGridTextColumn Header="Address" Binding="{Binding BillingAddress}" />
</DataGrid.Columns>
</DataGrid>
</ComboBox>
</Grid>
</Window>
代码:
public partial class MainWindow : Window, INotifyPropertyChanged
{
private ObservableCollection<Customer> _comboItems = new ObservableCollection<Customer>();
public MainWindow()
{
InitializeComponent();
ComboItems.Add(new Customer { CustomerName = "Steve", BillingAddress = "Address" });
ComboItems.Add(new Customer { CustomerName = "James", BillingAddress = "Address" });
}
public ObservableCollection<Customer> ComboItems
{
get { return _comboItems; }
set { _comboItems = value; }
}
}
public class Customer : INotifyPropertyChanged
{
private string _customerName;
private string _billingAddress;
public string CustomerName
{
get { return _customerName; }
set { _customerName = value; RaisePropertyChanged("CustomerName"); }
}
public string BillingAddress
{
get { return _billingAddress; }
set { _billingAddress = value; RaisePropertyChanged("BillingAddress"); }
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
结果: