Domanda

I have issued with WPF binding, I have ListBox binding to persons ObservableCollection with Textbox as DataTemplate that shows person, I want the Background of TextBox change from red to green if it red, and from green to red if it green, It changes but the ListBox doesn't show the changes, I have Raise the ObservableCollection but it's not work.

I have created a small new project please download HERE, and check what I miss.

After run the application type person Id in textbox (for example 1) and press the change color button, the color will change but the listbox dose not respond for that change.

Thanks in advance

È stato utile?

Soluzione

You should implement INotifyPropertyChanged interface also in People class:

public class People : INotifyPropertyChanged
{
    public int PersonID { get; set; }

    private string _fullName;
    public string FullName
    {
        get { return _fullName; }
        set { _fullName = value; OnPropertyChanged("FullName"); }
    }

    private bool _Status;
    public bool Status
    {
        get { return _Status; }
        set { _Status = value; OnPropertyChanged("Status"); }
    }

    private SolidColorBrush _statusColor;
    public SolidColorBrush StatusColor
    {
        get { return _statusColor; }
        set { _statusColor = value; OnPropertyChanged("StatusColor"); }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string name)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(name));
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top