There is a Progress Bar

<ProgressBar x:Name="progress" Height="50" Value="{Binding MyVar,UpdateSourceTrigger=PropertyChanged}" Minimum="0" Maximum="100"/> 

There is a property with which I want to associate the Value property public int MyVar { get; set; } public int MyVar { get; set; } public int MyVar { get; set; } When the MyVar property changes, nothing happens, although the idea is that Value should change along with MyVar, although I read a lot where it should work.

    1 answer 1

    The problem seems to be here:

     public int MyVar { get; set; } 

    You must implement INotifyPropertyChanged . Something like this:

     class VM : INotifyPropertyChanged { int myVar = 0; public int MyVar { get { return myVar; } set { if (myVar != value) { myVar = value; NotifyPropertyChanged(); } } } void NotifyPropertyChanged([CallerMemberName] string propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } public event PropertyChangedEventHandler PropertyChanged; } 
    • Thanks, it works, but should it not work without the implementation of this interface? - Faradey Inimicos
    • @FaradeyInimicos: See. Binding should somehow know that the value has changed? He can not just constantly re-read. There are two ways: implementing INotifyPropertyChanged and inheriting from DependencyObject + to encode the property as DependencyProperty . Both work, but it’s usually customary for VMs to use INotifyPropertyChanged . - VladD