Click here to Skip to main content
15,897,519 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to do data binding in wpf for combobox ia m using mvvm pattern. and beginner in data binding area
Posted

Your best bet is to use a CollectionView in your ViewModel, and bind your ComboBox to the CollectionView. The CollectionView has a CurrentChanged event which you can use to act on user selection changes, something like this (assuming your ComboBox Items are stored in a List<string>)

C#
class YourWindowViewModel
    {
        public CollectionView comboBoxItems { get; private set; }

        public YourWindowViewModel()
        {
            //Original list for ComboBox
            IList<string> ComboBoxList = new List<string>();
 
            comboBoxItems = new CollectionView(ComboBoxList);
            comboBoxItems.MoveCurrentTo(ComboBoxList[0]);
            comboBoxItems.CurrentChanged += new EventHandler(comboBox_CurrentChanged);
        }
 
        void comboBox_CurrentChanged(object sender, EventArgs e)
        {
            //Do work here if selection changes
        }
    }


Hope this helps
 
Share this answer
 
I would suggest using an observable property in your Viewmodel:

C#
private CollectionView _comboBoxOptions = null;
        public CollectionView comboBoxOptions
        {
            get { return _comboBoxOptions; }
            set
            {
                if (_comboBoxOptions != value)
                {
                    _comboBoxOptions = value;
                    RaisePropertyChanged("comboBoxOptions");
                }
            }
        }


You would also have to use INotifyPropertyChanged in the Viewmodel:

C#
public event PropertyChangedEventHandler PropertyChanged;
        protected void RaisePropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(name));
        }


Then in your XAML bind your control to that property :

XML
<ComboBox>
   <Binding Path="comboBoxOptions"
   ValidatesOnDataErrors="True"
   NotifyOnValidationError="True"
   Mode="OneWay"
   UpdateSourceTrigger="LostFocus"/>
</ComboBox>


You could initialise the comboBoxOptions property in the constructor for your Viewmodel.

Hope this helps

Laurence
 
Share this answer
 
v3
Hello
have a look on this code, it may help you.

C#
public class Order
{
    private readonly string _instrument;
    private readonly double _price;
    private readonly long _quantity;

    public Order(string instrument, double price, long quantity)
    {
        _instrument = instrument;
        _price = price;
        _quantity = quantity;
    }

    public string Instrument
    {
        get { return _instrument; }
    }

    public double Price
    {
        get { return _price; }
    }

    public long Quantity
    {
        get { return _quantity; }
    }
}

public void PopulateGrid(Grid grid)
{
    BindingList<order> orders = new BindingList<order>();

    orders.Add(new Order("Instrument1", 10.55, 34));
    orders.Add(new Order("Instrument2", 12.26, 154));
    orders.Add(new Order("Instrument1", 13.16, 14));
    orders.Add(new Order("Instrument5", 9.85, 52));
    orders.Add(new Order("Instrument1", 16.47, 11));

    grid.DataSource = orders;
}
</order></order>


more can be found here http://www.dapfor.com/en/net-suite/net-grid/features/hierarchical-data-binding[^]
 
Share this answer
 
Try following
C#
class YourWindowViewModel
    {
        public CollectionView comboBoxItems { get; private set; }
 
        public YourWindowViewModel()
        {
            
            List<yourclassname> listItems = new List<yourclassname>();
            YourComboBoxname.ItemsSource=listItems ;
         
        }
      
    }
</yourclassname></yourclassname>

Here is your xaml code
XML
<combobox>
   SelectedItem = "{Binding YourPropertyName}"
   DisplayMemberPath="{Binding YourPropertyName}"
   />
</combobox>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900