I have a window that uses a ReactiveUI Interaction to open a second window as a modal dialog, then return data from a ListBox in the second window.
The problem is that when .ShowDialog() completes, the ViewModel's SelectedItem always evaluates to null. I've confirmed that the binding is working correctly, and the selected item is getting updated in the dialog window's ViewModel properly. It's only when I return to the Interaction that the property resets to its default value (null).
I've put together a minimal example of the issue here:
https://github.com/replicaJunction/ReactiveDialogTest
The main logic under test is in MainWindowViewModel.cs.
Edit: Here's a snippet from the code with the basic idea:
GetNumberFromDialog = new Interaction<Unit, int>();
GetNumberFromDialog.RegisterHandler(interaction =>
{
var vm = new DialogWindowViewModel();
// Get a reference to the view for this VM
var view = Locator.Current.GetService<IViewFor<DialogWindowViewModel>>();
// Set its VM to our current reference
view.ViewModel = vm;
var window = view as Window;
var dialogResult = window.ShowDialog();
// At this point, vm.SelectedNumber is expected be the number the user selected -
// but instead, it always evaluates to 0.
if (true == dialogResult)
interaction.SetOutput(vm.SelectedNumber);
else
interaction.SetOutput(-1);
});
OpenDialog = ReactiveCommand.Create(() =>
{
GetNumberFromDialog.Handle(Unit.Default)
.Where(retVal => -1 != retVal) // If the dialog did not return true, don't update
.Subscribe(retVal =>
{
this.MyNumber = retVal;
});
});
Steps to reproduce the issue:
- Run the project. Note the label with the -5000 - this is the number to update.
- Click the "Open Dialog" button. A dialog window opens.
- Select a number in the ListBox. Note that the label under "Current Selection" updates - this is bound to the same value as the ListBox.SelectedItem.
- Click OK. The dialog closes.
Expected behavior: the label in the main window beneath "my number is" should update to the value you selected in the ListBox.
Actual behavior: the label updates to 0 (default value for int).
Why is my ViewModel resetting itself when the dialog closes?