Dynamically Populate ComboBox Within A WPF ListView With Items

后端 未结 1 1951
隐瞒了意图╮
隐瞒了意图╮ 2021-01-25 13:24

I have a WPF ListView which is bound to a data source. In the ListView are dynamically created ComboBoxes, which I would like to bind to another data source to provide the Item

相关标签:
1条回答
  • 2021-01-25 13:43
    1. You need a ViewModel for your scenario.

    Corresponding C# code ( you can use it as pseudo-code ) :

    public class ViewModel
        {
            public List<MyComboBoxItems> items { get; set; }
            public int outCustomValue2 { get; set; }
            public bool outCustomValue1 { get; set; }
    
            public ViewModel()
            {
                items = new List<MyComboBoxItems>();
                items.Add(new MyComboBoxItems());
                items.Add(new MyComboBoxItems());
                items.Add(new MyComboBoxItems());
                items.Add(new MyComboBoxItems());
            }
        }
    
        public class MyComboBoxItems
        {
            public List<MyComboBoxStrings> TestStrings { get; set; }       
    
            public MyComboBoxItems()
            {
                TestStrings = new List<MyComboBoxStrings>();
                TestStrings.Add(new MyComboBoxStrings("val1"));
                TestStrings.Add(new MyComboBoxStrings("val1"));
                TestStrings.Add(new MyComboBoxStrings("val1"));
                TestStrings.Add(new MyComboBoxStrings("val1"));
            }
        }
    
        public class MyComboBoxStrings
        {
            string _testString;
            public string TestString { get { return _testString; } }
    
            public MyComboBoxStrings(string value)
            {
                _testString = value;
            }
        }
    

    apply DataContext :

     ViewModel vm = new ViewModel();
     myList.DataContext = vm;
    
    0 讨论(0)
提交回复
热议问题