Reputation: 11734
Is it possible to notify changes on a child class? Like the way binding on ValueB is notified when changing ValueA?
The PropertyChangedEventHandler only allows a propertyname to be notified.
The only way I see is adding functionality to the Child class to call notification there (Notify method)..
public class Parent: INotifyPropertyChanged
{
public Child ChildA
{
get; set;
}
public Child ChildB
{
get; set;
}
public int ValueA
{
get
{
return _valueA;
}
set
{
_valueA = value;
OnPropertyChanged(nameof(ValueA));
}
}
public int ValueB
{
get
{
return _valueB;
}
set
{
_valueB = value;
OnPropertyChanged(nameof(ValueA));
OnPropertyChanged(nameof(ValueB));
}
}
public void RefreshBindings()
{
OnPropertyChanged(ChildA.Check);
OnPropertyChanged(ChildB.Check);
}
}
public class Child: INotifyPropertyChanged
{
public void Notify(string property)
{
OnPropertyChanged(property);
}
public bool Check
{
get
{
return // something;
}
}
}
Upvotes: 0
Views: 3126
Reputation: 2875
@NawedNabiZada I appreciate you suggestions but they do not work. Please only suggest it if you know for a fact they work.
Not sure what you tried, but my point is this:
<Grid>
<StackPanel>
<StackPanel Orientation="Horizontal">
<Label Content="Child A :"/>
<Label Content="{Binding Path=ChildA.Check}"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Content="Child B :"/>
<Label Content="{Binding Path=ChildB.Check}"/>
</StackPanel>
<Button Content="Check/UnCheck" Command="{Binding Path=RefreshBindingCommand}"/>
</StackPanel>
</Grid>
Parent:
public class Parent : INotifyPropertyChanged
{
public Child ChildA
{
get; set;
}
public Child ChildB
{
get; set;
}
public ICommand RefreshBindingCommand { get; }
public Parent()
{
ChildA = new Child(true);
ChildB = new Child(false);
RefreshBindingCommand = new RelayCommand(RefreshBindingCommand_Execute);
}
void RefreshBindingCommand_Execute(object obj)
{
RefreshBindings();
}
public void RefreshBindings()
{
ChildA.Notify(nameof(ChildA.Check));
ChildB.Notify(nameof(ChildB.Check));
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
Child:
public class Child : INotifyPropertyChanged
{
private bool _check;
public bool Check
{
get
{
_check = !_check;
return _check;
}
}
public Child(bool check)
{
_check = check;
}
public void Notify(string property)
{
OnPropertyChanged(property);
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
Upvotes: 3
Reputation: 169160
No, it's the source of the binding that should implement the INotifyPropertyChanged
interface and raise change notifications for the framework to be able to refresh the bindings "automatically".
So if you bind to ChildA.Check
of Parent
, it's the object returned by the ChildA
property (i.e. the Child
class) that should implement INotifyPropertyChanged
.
The other option would to bind to properties of Parent
that wraps properties of Child
, but the Child
must still somehow notify the parent when its state changes.
Upvotes: 2