WPF - PropertyChanged EventHandler not firing - c#

I'm having a problem with subscribing to PropertyChangedEventHandler event of a property on a bound instance of my class.
Here is the setup:
XAML:
<CheckBox IsChecked="{Binding MyObservableClassInstance.BooleanProperty}"/>
DataContext class property:
public MyObservableClass MyObservableClassInstance
{
get { return _myClassInstance; }
set
{
_myClassInstance= value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("MyObservableClassInstance"));
}
}
the initialisation and subscription to PropertyChanged event (this subscribed method is never reached):
MyObservableClassInstance = new MyObservableClass();
MyObservableClassInstance.PropertyChanged += OnMyObservableClassPropertyChanged; // <--- This method is never hit
my observable class: (the BooleanProperty is working normally with the the XAML checkbox binding)
public class MyObservableClass : INotifyPropertyChanged
{
bool _mybool = false;
public event PropertyChangedEventHandler PropertyChanged;
public bool BooleanProperty
{
get { return _mybool; }
set
{
_mybool = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("BooleanProperty")); // <--- This is reached normally on checking/unchecking the checkbox
}
}
}
So why is my OnMyObservableClassPropertyChanged method never reached upon Invoking the PropertyChanged event?

You have to attach (and detach) the handler method to the PropertyChanged event whenever the MyObservableClass property value changes.
public MyObservableClass MyObservableClassInstance
{
get { return _myClassInstance; }
set
{
if (_myClassInstance != null)
{
_myClassInstance.PropertyChanged -= OnMyObservableClassPropertyChanged;
}
_myClassInstance = value;
if (_myClassInstance != null)
{
_myClassInstance.PropertyChanged += OnMyObservableClassPropertyChanged;
}
PropertyChanged?.Invoke(
this, new PropertyChangedEventArgs(nameof(MyObservableClassInstance)));
}
}

Related

Fody.PropertyChanged: RaisePropertyChanged does not work for DependsOn Properties

For building the WPF application I used VS2022 (V17.4.4) and NuGet Fody.PropertyChanged Package (V4.1.0).
If a property is set directly, the PropertyChanged event will be raised for the property itself and all the [DependsOn] properties as well.
But if only the underlying field of the property is set and the PropertyChanged event is raised later manually by call of RaisePropertyChanged(nameof(Property)) the [DependsOn] properties will not raise any PropertyChanged event.
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace FodyPropertyChanged
{
[PropertyChanged.AddINotifyPropertyChangedInterface]
public class MainWindowViewModel : INotifyPropertyChanged
{
private DeviceStates _DeviceState;
private event PropertyChangedEventHandler _propertyChanged;
public event PropertyChangedEventHandler PropertyChanged
{
add { _propertyChanged += value; }
remove
{
if (_propertyChanged != null)
_propertyChanged -= value;
}
}
public DeviceStates DeviceState
{
get { return _DeviceState; }
set { _DeviceState = value; }
}
[PropertyChanged.DependsOn(nameof(DeviceState))]
public bool IsBusy
{
get { return DeviceState == DeviceStates.Working; }
}
public void SetDeviceStateTo(DeviceStates deviceState)
{
DeviceState = deviceState;
}
public void SetDeviceStateToAndRaisePropertyChangedAfterwards(DeviceStates deviceState)
{
_DeviceState = deviceState;
RaisePropertyChanged(nameof(DeviceState));
}
private void RaisePropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChangedEventHandler handler = _propertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
If I call SetDeviceStateTo(...) the PropertyChanged event will be raised for DeviceState and IsBusy (GUI will be updated, correctly). But if I call SetDeviceStateToAndRaisePropertyChangedAfterwards(...) the PropertyChanged event will be raised for DeviceState, only (IsBusy bindings will not be updated).
Any idea?
Like canton7 and BionicCode said:
[DependsOn] is only working for the setter not for events. So it is a known limitation and not a bug.

INotifyPropertyChanged does't work when field of property change internally

I try to binding textblock usercontrol with property of my class, but it only works at initial stage, I have implement IPropertyChnaged in my class.
In my class, _Feedbackpos (field of property) would change in background, I don't know how to solve this problem.
my class
public class TestControl : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyname)
{
if(PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyname));
}
}
private double _Feedbackpos;
public double Feedbackpos
{
get
{
return _Feedbackpos;
}
set
{
_Feedbackpos = value;
NotifyPropertyChanged("Feedbackpos");
}
}
//it's a callback function, it would excute when detect feedback position of controller change
private void ReadFeedbackpos()
{
_Feedbackpos = Controller.Read();
}
}
application windows
TestControl TestDll = new TestControl();
Binding BindingTxtBlk = new Binding(){Source= TestDll, Path = new Property("Feedbackpos")};
FeedbackPosTxtBlk.Setbinding(Textblock.TextProperty,BindingTxtBlk);
Change the function ReadFeedbackpos() to
private void ReadFeedbackpos()
{
Feedbackpos = Controller.Read();
}
Otherwise NotifyPropertyChanged("Feedbackpos"); will never get called.

C# Drop an item from a list if the item raise an event

Here's what I have now:
class MyClass
{
public string status;
private void DoSomething()
{
// do something and make change to this.status;
}
}
class MyClass2
{
public List<MyClass> MyClassLst;
private void DetectChangeInList()
{
// if the status property of an item in this.MyClassLst changed, remove this item from list
}
}
I have a List<MyClass>, and each of the MyClass will do some work and change the property status. I want to detect if any of the MyClass has its status changed and remove this item from MyClassLst.
I read about something on event but not very clearly about how to make it work.
If you need to be notified about changes to individual properties of each MyClass instance, it's not something that can magically happen.
Your MyClass will have to be responsible for firing an event whenever something changes (usually the PropertyChanged event, i.e. the INotifyPropertyChanged interface), and the other class will have to attach a handler to each item in the list to get notified.
C#6 has a couple of syntactic improvements which simplify this a bit, but you still have lots of work to do for each property:
public class Model : INotifyPropertyChanged
{
// this is the event which gets fired
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
// you need to raise the event in each property's setter
private _someValue;
public string SomeValue
{
get { return _someValue; }
set { if (value != _someValue) { _someValue = value; OnPropertyChanged(); } }
}
private _anotherVal;
public string AnotherValue
{
get { return _anotherVal; }
set { if (value != _anotherVal) { _anotherVal = value; OnPropertyChanged(); } }
}
}
In your case, it would be:
public class MyClass : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
// Never use public fields!
// Status should be a property which gets and sets
// the actual private backing field (_status).
private _status;
public string Status
{
get { return _status; }
set { if (value != _status) { _status = value; OnPropertyChanged(); } }
}
}
You will also most likely want to change List<MyClass> into your own implementation of ICollection<T> which will attach and detach these handlers as you add or remove items. It is usually done by deriving from Collection<T> and overriding relevant methods. If you are not comfortable with that, a slightly simpler approach might be to make the list private and expose Add/Remove and similar methods where you will attach/detach to the PropertyChanged event.

C# - Possible to detect when DataBound?

I would like to cache some UI-specific properties only at the time an object is DataBound, if it gets bound.
Say I have
public interface IAmA<T> ()
{
T Value {get;set;}
}
public class MyString : IAmA<string>, INotifyPropertyChanged {
private string _value = String.Empty;
public virtual string Value
{
get
{
return this._value;
}
set
{
this._value = value;
OnPropertyChanged();
}
}
public virtual event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
var e = new PropertyChangedEventArgs(propertyName);
handler(this, e);
}
}
}
Is it possible to implement an action or event to fire at the time the object is databound?
event Action OnDataBound { ... }
So I can detect, say, when
MyString myStrObj = new MyString("text");
myStrObj.OnDataBound += CacheUIElements();
Do.OtherStuff();
otherObj.DataSource = myStrObj; //causes CacheUIElements() to fire
You can very well do it in the property setter and call the Delegate.
In above case it will be for the OtherObj.DataSource. Because you want the Action to get triggered when you are setting the datasource. So it should be in the setter of whichever property where you want the action to trigger.
class OtherClass
{
DataSource
{
get{return _dataSource}
set
{
_dataSource = value;
OnPropertyChanged("DataSource");
OnDataBound();
}
}
}
If not, assuming OtherClass Implements OnPropertyChanged and triggers for DataSource like this, If its a framework element for sure it will implement NotifyPropertyChanged for DataSource because only then UI will refresh with the changes.
class OtherClass: INotifyPropertyChanged
{
DataSource
{
get{return _dataSource}
set
{
_dataSource = value;
OnPropertyChanged("DataSource");
}
}
}
Then you can subscribe to this event and check against the property name and trigger the OnDataBound(); e.g:
otherClassObj.PropertyChanged += OtherClassPropertyChanged()
private void OtherClassPropertyChanged(Obj sender, PropertyChangedEventargs e)
{
if(e.PropertyName = "DataSource")
{
OnDataBound();
}
}

PropertyChanged event handler is always null

When i first assign the bidning to the Label in its constructor, the Label binds correctly and displays correct information based on current ComponentData value of CurrentMarket class. However when ComponentData changes, OnPropertyChanged event fires ok, but the ProperyChanged handler is always NULL. Can someone kindly suggest what am i doing wrong?
I have a label and i set the binding like this:
public StyledLabel(string Property, int i)
{
Binding BindingText = new System.Windows.Data.Binding(Property);
BindingText.Source = Statics.CurrentMarket.ComponentData;
BindingText.Converter = new TextConverter();
this.SetBinding(Label.ContentProperty, BindingText);
}
Current market class looks like this:
public class CurrentMarket : INotifyPropertyChanged
{
string sMarket = "";
ComponentData cComponentData;
public string Market
{
set
{
sMarket = value;
OnPropertyChanged("Market");
ComponentData = SharedBoxAdmin.Components[sMarket];
}
get
{
return sMarket;
}
}
public ComponentData ComponentData
{
get { return cComponentData; }
set
{
cComponentData = value;
OnPropertyChanged("ComponentData");
}
}
public CurrentMarket()
{
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string info)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(info));
}
}
}
Thanks!
Try specifying the property name that you want to bind to as the Path of the Binding (rather than as part of the Source):
Binding BindingText = new System.Windows.Data.Binding(Property);
BindingText.Source = Statics.CurrentMarket;
BindingText.Path = new PropertyPath("ComponentData");
BindingText.Converter = new TextConverter();
this.SetBinding(Label.ContentProperty, BindingText);

Categories