WPF Binding in ComboBox with UserControl list

前端 未结 2 1657
囚心锁ツ
囚心锁ツ 2021-01-16 02:29

In two combobox A and B. A\'s ItemsSource is Custom list. and B\'s ItemsSource is UserControl list. When manually setting the SelectedItem, A combobox works well, but B comb

2条回答
  •  孤城傲影
    2021-01-16 03:11

    This is not the good way of achieving this. Better define the ItemTemplate for the combobox to have the UserControl in it like:

      
            
                
                    
                
            
        
    

    and define the class Item

    public class Item
    {
        public string ItemName { get; set; }
    }
    
    ObservableCollection _ItemsList = new ObservableCollection();
    public ObservableCollection ItemsList 
    {
        get
        {
            return _ItemsList ;
        }
        set
        {
            _ItemsList = value;
            OnPropertyChanged();
        }
    }
    

    Here DataContext of your UserControl will be Item object. you can bind the ItemName within you user control to show it in anyway you want.

    in your user control you can have:

     
    

提交回复
热议问题