Search code examples
c#wpfdata-bindingupdatesourcetrigger

Update Label when dataBind changes


I need to set a UpdateSourceTrigger, PropertyChanged from code behind but at this moment I have not found any way to do.

I am trying with the following code:

 factory.SetBinding(ContentProperty, new UpdateSourceTrigger("PropertyChanged"));

And my model

public class Event : INotifyPropertyChanged
{
    public string Name { get; set; }

    public byte Song { get; set; }

    public byte _currentSong;

    public byte CurrentSong
    {

        get { return _currentSong; }

        set
        {
            _currentSong = value;
            NotifyPropertyChanged();
        }
    }

    public GroupType Group { get; set; }

    public event PropertyChangedEventHandler PropertyChanged;

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

}

But I have the following mistake

Does not containt a constructor that takes 1 argument.

The mistake is clear, but I don't know (and don't find too much information) about how I have to do it.


Solution

  • The SetBinding method accepts a Binding object which has an UpdateSourceTrigger property:

    factory.SetBinding(ContentProperty, new Binding("CurrentSong") { UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged });