その3

```MainWindowViewModel
using Reactive.Bindings;
using System;

public class MainWindowViewModel
{
    public ReactiveProperty<string> InputText { get; } = new ReactiveProperty<string>();
    public ReactiveProperty<string> ResultText { get; } = new ReactiveProperty<string>();
    public ReactiveCommand<Unit> OpenChildWindowCommand { get; } = new ReactiveCommand<Unit>();

    public MainWindowViewModel()
    {
        OpenChildWindowCommand.Subscribe(() =>
        {
            var childViewModel = new ChildWindowViewModel();
            var childWindow = new ChildWindow(childViewModel);

            childWindow.Closed += (sender, e) =>
            {
                // サブウィンドウが閉じられたときの処理
                ResultText.Value = childViewModel.ChildResultText.Value;
            };

            childWindow.Show();
        });
    }
}
```
```ChildWindowViewModel
using Reactive.Bindings;
using System;

public class ChildWindowViewModel
{
    public ReactiveProperty<string> ChildInputText { get; } = new ReactiveProperty<string>();
    public ReactiveProperty<string> ChildResultText { get; } = new ReactiveProperty<string>();
    public ReactiveCommand<Unit> CloseWindowCommand { get; } = new ReactiveCommand<Unit>();

    public event EventHandler OnCloseRequested;

    public ChildWindowViewModel()
    {
        CloseWindowCommand.Subscribe(() =>
        {
            // ウィンドウを閉じるコマンドが実行されたときの処理
            ChildResultText.Value = ChildInputText.Value;
            OnCloseRequested?.Invoke(this, EventArgs.Empty);
        });
    }
}
```
```MainWindow
<Window x:Class="ReactivePropertyExample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ReactivePropertyExample"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TextBox Text="{Binding InputText, UpdateSourceTrigger=PropertyChanged}" />
        <Button Content="Open Child Window" Command="{Binding OpenChildWindowCommand}" />
        <TextBlock Text="{Binding ResultText}" />
    </Grid>
</Window>
```
```ChildWindow
<Window x:Class="ReactivePropertyExample.ChildWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ReactivePropertyExample"
        mc:Ignorable="d"
        Title="ChildWindow" Height="200" Width="300">
    <Grid>
        <TextBox Text="{Binding ChildInputText, UpdateSourceTrigger=PropertyChanged}" />
        <Button Content="Close Window" Command="{Binding CloseWindowCommand}" />
        <TextBlock Text="{Binding ChildResultText}" />
    </Grid>
</Window>
```
参考
https://shinshin-log.com/csharp-closewindow-fromvm/
https://koyak.hatenadiary.org/entry/20120902/1346557120
https://kmua.hatenablog.com/entry/2021/11/16/235013