WPF将Combobox属性绑定到不同的数据源

时间:2018-07-01 23:29:30

标签: wpf combobox selectedindex

我想了解如何为组合框设置不同的绑定源。例如:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        customers cs = new customers();
        itcModels.ItemsSource = cs.list;
    }
}

public class CarModels
{
    public ObservableCollection<car> list { get; set; }
    public CarModels()
    {
        list = new ObservableCollection<car>();
        list.Add(new car() { model = "Porsche" });
        list.Add(new car() { model = "BMW" });
        list.Add(new car() { model = "Audi" });
        list.Add(new car() { model = "Mercedes" });
    }
}
public class car
{
    public string model { get; set; }
}
public class customers
{
    public ObservableCollection<customer> list { get; set; }
    public customers()
    {
        list = new ObservableCollection<customer>();
        list.Add(new customer() { hisCar = 3 });
        list.Add(new customer() { hisCar = 1 });
        list.Add(new customer() { hisCar = 2 });
        list.Add(new customer() { hisCar = 0 });
    }
}
public class customer
{
    public int hisCar { get; set; }
}

CustomControlCustomer.xaml

<Grid>
    <ComboBox x:Name="cboCustomerCar" ItemsSource="{Binding list}" DisplayMemberPath="model"/>
</Grid>

CustomControlCustomer.cs

 public partial class ccCustomer : UserControl
{
    public ccCustomer()
    {
        InitializeComponent();
        cboCustomerCar.DataContext = new CarModels();

    }
}

我想执行以下操作: 1.将组合框列表绑定到CarModels.list,以便所有模型都在列表中(已在代码中完成) 2.设置(或更好:)将每个组合框的选定索引绑定到每个客户。hiscar

任何想法我该怎么做?

0 个答案:

没有答案