Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Updating Observable collection with combo box inside a user control

Updating Observable collection with combo box inside a user control

Scheduled Pinned Locked Moved WPF
visual-studiocsharpwpflinqcom
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dashingsidds
    wrote on last edited by
    #1

    Hi Experts, I have a combo box inside a user control and i am trying to update an observable collection with the change event of this combo box. This combo box has a list view inside it. I have added a working example of this scenario so you can copy paste it in your VS IDE. The user control in this example is just a part of the original user control. I have removed the code which are not required. --Customer.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ComponentModel;

    namespace TestMVVM
    {
    class Customer : INotifyPropertyChanged
    {

        public int ID { get; set; }
        public int NumberOfContracts { get; set; }
    
        private string firstName;
        private string lastName;
    
        public string FirstName
        {
            get { return firstName; }
            set
            {
                if (firstName != value)
                {
                    firstName = value;
                    RaisePropertyChanged("FirstName");
    
                }
            }
        }
    
        public string LastName
        {
            get { return lastName; }
            set
            {
                if (lastName != value)
                {
                    lastName = value;
                    RaisePropertyChanged("LastName");
                }
            }
        }
    
        #region PropertChanged Block
        public event PropertyChangedEventHandler PropertyChanged;
    
        private void RaisePropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }
        #endregion
    }
    

    }

    --CustomerHeaderViewModel class

    class CustomerHeaderViewModel
    {
        public ObservableCollection Customers { get; set; }
    
        public void LoadCustomers()
        {
            ObservableCollection customers = new ObservableCollection();
    
            //this is where you would actually call your service
            customers.Add(new Customer { ID = 1, FirstName = "Jim", LastName = "Smith", NumberOfContracts = 23 });
    
            Customers = customers;
        }
    }
    

    -- UCComboBox.xaml User Control

    <UserControl x:Class="TestMVVM.UCComboBox"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/20

    K 1 Reply Last reply
    0
    • D dashingsidds

      Hi Experts, I have a combo box inside a user control and i am trying to update an observable collection with the change event of this combo box. This combo box has a list view inside it. I have added a working example of this scenario so you can copy paste it in your VS IDE. The user control in this example is just a part of the original user control. I have removed the code which are not required. --Customer.cs

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.ComponentModel;

      namespace TestMVVM
      {
      class Customer : INotifyPropertyChanged
      {

          public int ID { get; set; }
          public int NumberOfContracts { get; set; }
      
          private string firstName;
          private string lastName;
      
          public string FirstName
          {
              get { return firstName; }
              set
              {
                  if (firstName != value)
                  {
                      firstName = value;
                      RaisePropertyChanged("FirstName");
      
                  }
              }
          }
      
          public string LastName
          {
              get { return lastName; }
              set
              {
                  if (lastName != value)
                  {
                      lastName = value;
                      RaisePropertyChanged("LastName");
                  }
              }
          }
      
          #region PropertChanged Block
          public event PropertyChangedEventHandler PropertyChanged;
      
          private void RaisePropertyChanged(string property)
          {
              if (PropertyChanged != null)
              {
                  PropertyChanged(this, new PropertyChangedEventArgs(property));
              }
          }
          #endregion
      }
      

      }

      --CustomerHeaderViewModel class

      class CustomerHeaderViewModel
      {
          public ObservableCollection Customers { get; set; }
      
          public void LoadCustomers()
          {
              ObservableCollection customers = new ObservableCollection();
      
              //this is where you would actually call your service
              customers.Add(new Customer { ID = 1, FirstName = "Jim", LastName = "Smith", NumberOfContracts = 23 });
      
              Customers = customers;
          }
      }
      

      -- UCComboBox.xaml User Control

      <UserControl x:Class="TestMVVM.UCComboBox"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/20

      K Offline
      K Offline
      Kunal Chowdhury IN
      wrote on last edited by
      #2

      Hi Samar, From the first look of your query I will suggest you to use "DataBinding" for the ComboBox. As you are using ObservableCollection, you don't have to explicitly fire the refresh event. Once data changed in collection it will refresh implicitly. Next thing is, use Mode=TwoWay while binding to the properties inside your combobox. Use a DataTemplate to create your specific template. It will be easy for you to bind the values. Have a look & get back to me if any issues.

      Don't forget to Click on [Vote] and [Good Answer] on the posts that helped you.


      Regards - Kunal Chowdhury | Software Developer | Chennai | India | My Blog | My Tweets | Silverlight Tutorial

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups