Best practice for class bindings? #15148
-
I'm working with a project in which, for xaml simplicity reasons, I packed some control properties into a class, like: public class MyControlProperty : ObservableObject
{
[ObservableProperty]
int field1;
[ObservableProperty]
int field2;
} And I redirected the property change of MyControlProperties onto the view model: class MainViewModel : ViewModelBase
{
public MainViewModel()
{
MyProperties.PropertyChanged += (sender, e) => OnPropertyChanged(nameof(MyProperties));
}
[ObservableProperty]
MyControlProperty myProperties;
} And in xaml I bind it to my own control: <MyControl CustomProperty="{Binding MyProperties}" /> Then, when I change a field in MainViewModel.MyProperties.Field1, the OnPropertyChanged event of MainViewModel.MyProperties is indeed called as expected. But however, MyControl doesn't update. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
the class should be public or at least internal. |
Beta Was this translation helpful? Give feedback.
-
Have you tried not raising the change event for |
Beta Was this translation helpful? Give feedback.
Have you tried not raising the change event for
myProperties
? I am not 100% sure how Avalonia works internally but I suspect it might be unsubscribing from changes thatmyProperties
itself raises. Semantically speaking: raising the event formyProperties
means the property itself is changing, not the child properties.