WPF组合框结合 [英] wpf combobox binding
问题描述
嗨我正尝试一个List&LT结合;>到组合框
Hi I´m trying to bind a List<> to a combobox.
<ComboBox Margin="131,242,275,33" x:Name="customer" Width="194" Height="25"/>
public OfferEditPage()
{
InitializeComponent();
cusmo = new CustomerViewModel();
DataContext = this;
Cusco = cusmo.Customer.ToList<Customer>();
customer.ItemsSource = Cusco;
customer.DisplayMemberPath = "name";
customer.SelectedValuePath = "customerID";
customer.SelectedValue = "1";
}
我变得没有错误,但组合框总是空空的。
库斯科是我列出的财产。
我不知道什么是错与此code。
你能帮助我吗?
I become no Error but the Combobox is always empty. Cusco is the Property of my List. I have no idea whats wrong with this code. Can you help me?
问候
public class Customer
{
public int customerID { get; set; }
public string name { get; set; }
public string surname { get; set; }
public string telnr { get; set; }
public string email { get; set; }
public string adress { get; set; }
}
这是客户类,这是我的模型。
this is the Customer Class which is my model.
public class CustomerViewModel
{
private ObservableCollection<Customer> _customer;
public ObservableCollection<Customer> Customer
{
get { return _customer; }
set { _customer = value; }
}
public CustomerViewModel()
{
GetCustomerCollection();
}
private void GetCustomerCollection()
{
Customer = new ObservableCollection<Customer>(BusinessLayer.getCustomerDataSet());
}
}
这就是视图模型。
and this is the ViewModel.
推荐答案
试着用一个实际的绑定对象设置ItemsSource属性
Try setting the ItemsSource property with an actual Binding object
XAML方法(推荐):
XAML Method (recommended):
<ComboBox
ItemsSource="{Binding Customer}"
SelectedValue="{Binding someViewModelProperty}"
DisplayMemberPath="name"
SelectedValuePath="customerID"/>
编程方法:
Binding myBinding = new Binding("Name");
myBinding.Source = cusmo.Customer; // data source from your example
customer.DisplayMemberPath = "name";
customer.SelectedValuePath = "customerID";
customer.SetBinding(ComboBox.ItemsSourceProperty, myBinding);
此外,在您的客户属性的setter应提高PropertyChanged事件
Also, the setter on your Customer property should raise the PropertyChanged event
public ObservableCollection<Customer> Customer
{
get { return _customer; }
set
{
_customer = value;
RaisePropertyChanged("Customer");
}
}
如果上述方法不工作,尝试从构造函数的结合部分移动到OnLoaded重写方法。在加载页面时,它可能会重置值。
If the above does not work, try moving the binding portion from the constructor to the OnLoaded override method. When the page loads, it may be resetting your values.
这篇关于WPF组合框结合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!