Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i can't able to update my custom control properties by using binding.my custom control properties are take the value from the data context for the first object of data context.if data context is changed my properties doesn't take the value from the updated data context.please help me to let me know about my problem
Posted

1 solution

Whenever you change a property in the data context object (normally this is called a view model), you have to raise a property change.

Your view model class has to look similar as the following
C#
class MyViewModel : INotifyPropertyChanged
{
    private int _aValue;
    public int AValue {
        get { return _aValue; }
        set {
            if (_aValue != value) {
                _aValue = value;
                RaisePropertyChanged();
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChanged([CallerMemberName] string propertyName = "")
    {
        var handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}


Whenever you change a property you have to call the event handlers of PropertyChanged.
The above solution uses .NET 4.5 CallerMemberName annotation which uses the name of the setter property. When you use .NET 4, just remove the annotation and add the property name as a string.

Further reading:
- https://msdn.microsoft.com/en-us/library/vstudio/ms743695(v=vs.100).aspx[^]
- Asking Google "view model property changes"
- Asking Codeproject "wpf viewmodel view"
 
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