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 1035135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:30:01+00:00 2026-05-16T14:30:01+00:00

I’m building a WPF MVVM application and I’d like to keep what I can

  • 0

I’m building a WPF MVVM application and I’d like to keep what I can conforming to MVVM (I know some stuff is over-engineering, putting a best effort here though).

I’ve run into a small dillema, I need to Hide / Close a window based on some logic in one the the methods in the view model.

I can’t for the life of me come up with a reliable way to do it. I’ve bound a Visibility property to the window’s Visibility DP, which works (sorta) but how would I call Close() on the dialog?

I’m using View-First MVVM so the view model has no knowledge of the view, the view instantiates a view-model (through DI/IoC) and sets it as the DataContext.

Commands work the wrong way, events are out of the question unless I bind to it in code behind which is something id rather not do if there’s an MVVM way to do it that isn’t entirely convoluted.

Any ideas from the greater SO community?

Or maybe I’m missing something about MVVM in general? Either way, let me know 😮

  • 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-16T14:30:02+00:00Added an answer on May 16, 2026 at 2:30 pm

    As is often the case, there are many ways to skin this cat.

    One way to do this without codebehind in the view is to use an attached behaviour, such as the following:

    public static class CloseBehavior 
    {
        public static bool GetCloseWhen(DependencyObject obj)
        {
            return (bool)obj.GetValue(CloseWhenProperty);
        }
    
        public static void SetCloseWhen(DependencyObject obj, bool value)
        {
            obj.SetValue(CloseWhenProperty, value);
        }
    
        public static readonly DependencyProperty CloseWhenProperty =
            DependencyProperty.RegisterAttached(
                "CloseWhen", typeof(bool), typeof(CloseBehavior), 
                new UIPropertyMetadata(OnCloseWhenChanged));
        // the lone parameter in the UIPropertyMetadata is a callback
        // for when the property value changes
    
        static void OnCloseWhenChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            // if false, we're not concerned with it
            if(!(bool)e.NewValue) return;
    
           // if attached to something other than a window, this doesn't make sense
            var win = d as Window;
    
            if(d == null) return;
    
                // close the window
            win.Close();
        }
    }
    

    In your XAML:

    <Window x:Class="WpfApplication1.Window1"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
         xmlns:local="clr-namespace:WpfApplication1" 
    
         local:CloseBehavior.CloseWhen="{Binding ViewModelWorkComplete}"
    >
    <! -- content -->
    </Window>
    

    Where ViewModelWorkComplete is just a boolean property in the view model.

    The effect is that when the viewmodel sets ViewModelWorkComplete to true (and raises the appropriate INotifyPropertyChanged event), the window will be closed.

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

Sidebar

Related Questions

No related questions found

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.