0

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>
Cor
  • 389
  • 1
  • 2
  • 14
iljon
  • 398
  • 2
  • 16
  • 1
    Please supply more code, at this point we can only guess what is the issue, also what is the exact exception that is thrown? – Ravid Goldenberg Mar 04 '16 at 09:18
  • A dialog cannot be invisible. As soon as you make it invisible then it will close. Necessarily so, the user would not be able to continue to interact with the rest of the disabled windows and lose control over the app. So *somehow* you are calling DoSubit() even after the dialog is not around anymore. That is a bug. – Hans Passant Mar 04 '16 at 10:32
  • DoSubmit() function is called via Command that is binded to Button. Is it possible this is called two times? – iljon Mar 04 '16 at 11:23

0 Answers0