In my WPF application is some cases setting DialogResult = true raises an Exception telling that DialogResult cannot be set on unvisible window. Window is launched:
Window.ShowDialog()
Neither Window.Close() nor DialogResult is not called before. How to ensure that
DialogResult = true;
can be executed without raising error?
The exact exception content is:
DialogResult can be set only after Window is created and shown as dialog.
Here is the code
// FormConstructor
public FrmAccept()
{
InitializeComponent();
vm = new AcceptViewModel();
vm.CmdSubmit = new RelayCommand(pars => DoSubmit(), pars => vm.SubmitCanExecute);
this.DataContext = vm;
}
private void DoSubmit()
{
try
{
if (!vm.IsGuiEnabled)
{
this.Close();
}
else
{
vm.IsGuiEnabled = false;
bool isOk = DBervice.SaveDB(vm.Data);
if (isOk)
{
PrintData(); // This can cause an Exception
}
if (iOk)
{
DialogResult = true; // << === Here Exception Raises in Some Cases
this.Close();
}
else
{
vm.IsGuiEnabled = true;
}
}
}
catch (Exception ex)
{
vm.IsGuiEnabled = true;
}
}
And part of XAML
<Button x:Name="btnAccept"
Style="{StaticResource FlatButtonLarge}"
IsEnabled="{Binding Path=IsGuiEnabled}"
Height="42"
Content="Submit">
<Button.InputBindings>
<MouseBinding Gesture="LeftClick" Command="{Binding Path=CmdSubmit}" />
</Button.InputBindings>
</Button>