Combobox INotifyPropertyChanged event not raised!!!

Posted by nagiah on Stack Overflow See other posts from Stack Overflow or by nagiah
Published on 2010-05-26T06:37:03Z Indexed on 2010/05/26 6:41 UTC
Read the original article Hit count: 249

I created a combobox and set observable collection as the itemsource and implemented INotifyPropertyChanged on the observable collection item. Even after that, when I select different item in the combobox, the OnPropertyChange method is not invoked. I think I am not making the binding properly. Could any one please correct me/ suggest me in this regard.

---------------------------------MainPage.xaml---------------------------------------------------

<StackPanel Width="300">

        <ComboBox Name="cboName"></ComboBox>

        <TextBox Name="tbxName" Text="{Binding Path=name,Mode=TwoWay,ElementName=cboName}" ></TextBox>


</StackPanel>

---------------------------MainPage.xaml.cs-----------------------------------------------

using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using System.Collections.ObjectModel; using System.Collections.Specialized; using System.ComponentModel;

namespace MasterDetailsUpdate {

public partial class MainPage : UserControl
{
    public MainPage()
    {
        InitializeComponent();
        Loaded += new RoutedEventHandler(MainPage_Loaded);
    }

    void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
        ObservableCollection<Person> persons = new ObservableCollection<Person>();
        persons.Add(new Person { city = "c1", name = "n1" });
        persons.Add(new Person { city = "c2", name = "n2" });
        persons.Add(new Person { city = "c3", name = "" });
        persons.Add(new Person { city = "c4", name = "" });
        persons.Add(new Person { city = "c5", name = "n1" });

        cboName.ItemsSource = persons;
        cboName.DisplayMemberPath = "name";
    }       
}

public class Person : INotifyPropertyChanged
{
    private string _name;
    private string _city;
    public string name
    {
        set
        {
            _name = value;
            OnPropertyChanged("name");
        }
        get
        {
            return _name;
        }
    }


    public string city
    {
        set
        {
            _city = value;
            OnPropertyChanged("city");
        }
        get
        {
            return _city;
        }
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    #endregion
}

}


Thank You

© Stack Overflow or respective owner

Related posts about databinding

Related posts about silverlight-3.0