C# wpf 组合框绑定

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11056196/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 16:19:29  来源:igfitidea点击:

wpf combobox binding

c#wpflistbindingcombobox

提问by Veeesss

Hi I′m trying to bind a List<> to a combobox.

嗨,我正在尝试将 List<> 绑定到组合框。

<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";
    }

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?

我变得没有错误,但组合框总是空的。库斯科是我名单的财产。我不知道这段代码有什么问题。你能帮助我吗?

Greets

问候

 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.

这就是 ViewModel。

采纳答案by Steve Konves

Try setting the ItemsSource property with an actual Binding object

尝试使用实际的 Binding 对象设置 ItemsSource 属性

XAML Method (recommended):

XAML 方法(推荐):

<ComboBox
    ItemsSource="{Binding Customer}"
    SelectedValue="{Binding someViewModelProperty}"
    DisplayMemberPath="name"
    SelectedValuePath="customerID"/>

Programmatic method:

程序化方法:

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);

Also, the setter on your Customer property should raise the PropertyChanged event

此外,您的 Customer 属性上的 setter 应该引发 PropertyChanged 事件

public ObservableCollection<Customer> Customer
{
    get { return _customer; }
    set
    {
        _customer = value;
        RaisePropertyChanged("Customer");
    }
}

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.

如果上述方法不起作用,请尝试将绑定部分从构造函数移动到 OnLoaded 覆盖方法。当页面加载时,它可能会重置您的值。

回答by klaverty

As an expansion on Steve's answer,

作为史蒂夫答案的扩展,

You need to set the datacontext of your form.

您需要设置表单的数据上下文。

Currently you have this:

目前你有这个:

InitializeComponent();
cusmo = new CustomerViewModel();
DataContext = this;

It should be changed to this:

应该改成这样:

InitializeComponent();
cusmo = new CustomerViewModel();
DataContext = cusmo;

Then as Steve noted you will need another property on the viewmodel to store the selected item.

然后正如史蒂夫指出的那样,您将需要视图模型上的另一个属性来存储所选项目。