Add comboBox items from code behind. [WPF]

前端 未结 3 1115
一整个雨季
一整个雨季 2021-01-12 19:06

I grabbed this code from MSDN. What Im trying to do is similar, but use a list instead of three different strings. so say

List strList = new Li         


        
3条回答
  •  庸人自扰
    2021-01-12 20:00

    If you don't want to do this following a binding/mvvm pattern, it is quite simple to just do the following:

    foreach (var item in items)
    {
        _comboBox.Items.Add(item);
        _comboBox.SelectedValuePath = "ID";
        _comboBox.DisplayMemberPath = "Name";
    }
    

    Which can be accessed later like so:

    var id = _comboBox.SelectedValue;
    

提交回复
热议问题