Replies: 3 comments 1 reply
-
Override OnPropertyChanged. That would also allow you to unsubscribe the event handler if you changed the viewmodel. protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
base.OnPropertyChanged(change);
if (change.Property == DataContextProperty)
{
// your code here
}
} There is also a DataContextChanged event but it does not give you the old value. |
Beta Was this translation helpful? Give feedback.
-
I believe that DataContextChanged event is designed for this purpose public partial class MainView : UserControl
{
public MainView()
{
InitializeComponent();
this.DataContextChanged += (t, e)=>{
if (DataContext is MainViewModel model)
{
//TODO: Initialize the model or perform any setup needed
}
};
}
} |
Beta Was this translation helpful? Give feedback.
-
When it's available depends on the pattern(s) you're using. You're responsible for wiring the VM to I basically use the same as I found the extra code overhead of |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I am working on a MVVM Avalonia application and got stuck.
My Mainwindow has to set an event handler
the Problem I have is, that
DataContext
and therefore_vm
isnull
. TheDataContext
is set in the fileApp.axml.cs
and is available not before the constructor ofMainWindow
has been finished.Where to put
_vm.Eventhandler += (t, e)=>{ ... }
?Joachim
Beta Was this translation helpful? Give feedback.
All reactions