Reputation: 31
I have two views bound to the same view model. In the main window view model class, a second window is created and opened. How to hide the second window when its button is clicked? A hide() command does not work when bound to a button in the second view, but the window is hidden if the same command is bound to a button in the main window.
How to hide the second window? Function in MainWindowViewModel:
public void DeclineSettingsChanges()
{
SW.Hide();
}
Reactive command:
private ReactiveCommand<Unit, Unit> DeclineSettingsChangesCommand { get; }
Creting command in constructor:
DeclineSettingsChangesCommand = ReactiveCommand.Create(DeclineSettingsChanges);
SettingsWindow.axaml part:
<Button Content="Cancel" Command="{Binding DeclineSettingsChanges, Source={StaticResource MainWindowViewModel}}"/>
Similarly, the binded button in MainWindow.axaml works as it should and hides the SettingsWindows.
Upvotes: 0
Views: 1134
Reputation: 10206
Windows are closed using the Close()
method, personally, I've never used the Hide()
method on a Window
so not entirely sure of it's internal workings.
You may find it helpful to use a library for dialogs and Windows. I've written a small simple library that you can get here:
https://github.com/jamsoft/JamSoft.AvaloniaUI.Dialogs
Upvotes: 0