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

  • Home
  • SEARCH
  • 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 6069763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:51:26+00:00 2026-05-23T09:51:26+00:00

I am showing a window. The instance is created and shown within the ViewModel

  • 0

I am showing a window. The instance is created and shown within the ViewModel (bad practice I know…)

NewWindow form = new NewWindow();
form.ShowDialog(); 

Within that form I have an OK_button which is doing stuff when it is pressed. There exist a ViewModel to this form which has the OK Command from the OK_Button.
After that button is pressed doing stuff I want to close that form programatically from within the viewmodel. How can I do that?

I use WPF

UPDATE

now lets see what I do wrong: Here the DataContext event is not fired although my Window with the ViewModel is shown!?

The window that is shown and must be closed from the ViewModel:

public partial class NewSchoolYearWindow : Window
    {
        public NewSchoolYearWindow()
        {
            InitializeComponent();
        }

        private void Window_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            NewSchoolYearViewModel vm = (NewSchoolYearViewModel)e.NewValue;
            vm.CloseNewSchoolYearDialog += () => this.Close();              
        }
    }

Why is the DataContextChanged even not fired?

I use this XAML in my Window:

<Window x:Class="TBM.View.NewSchoolYearWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:ViewModel="clr-namespace:TBM.ViewModel"
        Title="Start a new school year"
        Height="412" Width="505" 
        WindowStartupLocation="CenterScreen"        
        WindowStyle="ThreeDBorderWindow"
        ResizeMode="CanResize" DataContextChanged="Window_DataContextChanged">
    <Window.Resources>

        <ViewModel:NewSchoolYearViewModel x:Key="NewSchoolYearViewModelID" />

    </Window.Resources>

    <Grid DataContext="{Binding ., Source={StaticResource NewSchoolYearViewModelID}}" Name="MainGrid">
        <TextBlock Height="27" HorizontalAlignment="Left" Margin="68,46,0,0" Name="textBlock1" Text="School year start" VerticalAlignment="Top" Width="98" />
        <TextBlock Height="27" HorizontalAlignment="Left" Margin="68,93,0,0" Name="textBlock2" Text="School year end" VerticalAlignment="Top" Width="98" />
        <TextBlock Height="27" HorizontalAlignment="Left" Margin="68,169,0,0" Name="textBlock4" Text="Database name:" VerticalAlignment="Top" Width="150" TextAlignment="Left" TextTrimming="CharacterEllipsis" />
        <TextBlock Height="27" HorizontalAlignment="Left" Margin="68,215,0,0" Name="textBlock3" Text="Directory:" VerticalAlignment="Top" Width="63" TextAlignment="Left" TextTrimming="CharacterEllipsis" />
        <TextBox IsReadOnly="True" Text="{Binding CurrentSchoolYear.Directory}"  Height="23" HorizontalAlignment="Left" Margin="172,212,0,0" Name="textBox3" VerticalAlignment="Top" Width="224" />
        <Button Command="{Binding OpenNewSchoolYearDialogCommand}" Content="DIR" Height="23" HorizontalAlignment="Right" Margin="0,211,27,0" Name="button1" VerticalAlignment="Top" Width="54" />
        <Button Command="{Binding CreateNewSchoolYearCommand}" Content="OK" Height="23" HorizontalAlignment="Left" Margin="381,299,0,0" Name="button2" VerticalAlignment="Top" Width="75" />
        <Button Content="Cancel" Height="23" HorizontalAlignment="Left" Margin="300,299,0,0" Name="button3" VerticalAlignment="Top" Width="75" />
        <DatePicker Height="25" HorizontalAlignment="Left" Margin="172,42,0,0" SelectedDate="{Binding CurrentSchoolYear.Start}" SelectedDateFormat="Long" VerticalAlignment="Top" Width="175" />
        <DatePicker Height="25" HorizontalAlignment="Left" Margin="172,89,0,0" SelectedDate="{Binding CurrentSchoolYear.End}" SelectedDateFormat="Long" VerticalAlignment="Top" Width="175" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="172,166,0,0" Name="textBox1" Text="{Binding CurrentSchoolYear.Name}" VerticalAlignment="Top" Width="175" />
    </Grid>
</Window>
  • 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-23T09:51:27+00:00Added an answer on May 23, 2026 at 9:51 am

    Declare an event in the ViewModel:

    public event EventHandler<CloseRequestedEventArgs> CloseRequested;
    
    protected virtual void OnCloseRequested(bool? dialogResult)
    {
        var handler = CloseRequested;
        if (handler != null)
            handler(this, new CloseRequestedEventArgs(dialogResult));
    }
    
    ...
    
    public class CloseRequestedEventargs : EventArgs
    {
        private readonly bool? _dialogResult;
    
        public CloseRequestedEventargs(bool? dialogResult)
        {
            _dialogResult = dialogResult;
        }
    
        public bool DialogResult { get { return _dialogResult; } }
    }
    

    And handle it in the code-behind:

    var vm = (MyViewModel)DataContext;
    vm.CloseRequested += vm_CloseRequested;
    
    ...
    
    private void vm_CloseRequested(object sender, CloseRequestedEventArgs e)
    {
        if (e.DialogResult.HasValue)
            this.DialogResult = e.DialogResult; // sets the dialog result AND closes the window
        else
            this.Close();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

DevExpress XtraReports has a predefined window for showing up the reports that is bundle
I'm generating and showing a new WinForms window on top of a Main Window.
How can I hide a Gtk# window that I created in MonoDevelop? I tried
I have a page viewer as main window showing an external page as we
In showing a colleague how to use SVN yesterday, we created a test folder
I have a costumer showing Notepad with a large set of data that looks
I have a form showing progress messages as a fairly long process runs. It's
i'm developing an application (wpf) that have 3 windows. in the main window user
After showing an XPS file in the WPF DocumentViewer, and closing the DocumentViewer instance,
If I only have one window showing in emacs and use M-x compile, the

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.