Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7488793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:02:20+00:00 2026-05-29T15:02:20+00:00

I am trying to implement this MVVM pattern for the WPF form closing which

  • 0

I am trying to implement this MVVM pattern for the WPF form closing which is also explained in this blog and I am getting System.InvalidOperationException with error message “DialogResult can be set only after Window is created and shown as dialog.” when I am trying to set the Dialog Result on Close button command:

DialogResult = true;

Here is my ViewModel:

class MainWindowViewModel:INotifyPropertyChanged
{
    private bool? dialogResult;
    public bool? DialogResult
    {
        get { return dialogResult; }
        set
        {
            if (value != this.dialogResult)
            {
                this.dialogResult = value;
                OnPropertyChanged("DialogResult");
            }
        }
    }

    public string Text
    {
        get { return "Hello!"; }
    }

    void CloseCommandExecute()
    {
        this.DialogResult = true;
    } 

and Here is the XAML View:

<Window x:Class="WpfApplication.Mvvm.Windowclosing.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication.Mvvm.Windowclosing"
        local:DialogCloser.DialogResult="{Binding DialogResult}"
        Title="MainWindow" Height="350" Width="525">
    <Window.DataContext>
        <local:MainWindowViewModel />
    </Window.DataContext>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="100"/>
            <RowDefinition Height="40"/>
        </Grid.RowDefinitions>
        <TextBlock Text="{Binding Text}" Grid.Row="0"/>
        <Button Grid.Row="1" Command="{Binding CloseCommand}">Close Me</Button>
    </Grid>
</Window>

What am I doing wrong here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T15:02:22+00:00Added an answer on May 29, 2026 at 3:02 pm

    Setting a dialog result only works when you open your form with ShowDialog(). You get this error when you try to set the dialog result on a form opened with Show().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement the MVVM pattern in WPF. I've followed Jeremy Alles's Very
I'm trying to implement a WPF application using MVVM (Model-View-ViewModel) pattern and I'd like
So I've been trying to implement the MVVM pattern within a simple WPF application
I'm trying to implement this pattern in my WinForms application (I don't like it,
I've been tinkering around with the MVVM pattern and now I'm trying to implement
I am trying to implement MVVM in a new project (based on WPF &
I'm trying to implement this code for accelerometer use, but I'm getting a compiler
I'm trying to implement Menu in a Window using MVVM pattern. So I have
I am trying to implement a WPF MVVM application. And I am a beginner
I'm trying to implement this method of making a nice form upload: www.kavoir.com/2009/02/styling-file-upload-select-input-control-input-typefile.html/ which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.