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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T20:02:10+00:00 2026-06-18T20:02:10+00:00

I’m working on a WPF MVVM application. I’m showing some data in a datagrid.

  • 0

I’m working on a WPF MVVM application. I’m showing some data in a datagrid. I’ve two buttons to Add and Edit the selected record. I’ve data in ViewModel and I’ve to show another window (view) and make sure that ViewModels should have no information about views.
Where should I create its view and viewmodel?
How to get the data back and update datagrid?
How can I achieve this in MVVM?
We have not yet decided to use any framework, so I’ve to create my own interface.

  • 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-06-18T20:02:11+00:00Added an answer on June 18, 2026 at 8:02 pm

    Note: This ended up being quite a long answer – please ask me if anything is unclear

    The implementation of dialog windows is a contentious issue in MVVM designs, and different people use different approaches.

    Like you, I’ve decided not to use any framework and implement most things by hand. When it comes to dialog windows, I choose to be pragmatic about my implementation of MVVM, by launching the Dialog Window from inside my ViewModel. Also, I allow each Dialog ViewModel to have a reference to the Window it is displayed in, so it can close it when appropriate (details below). This breaks some of the strict MVVM “rules”, but it gets the job done.

    The main downside of this is that it might break unit testing if you are testing something that goes through a dialog. However, you can go a long way without running into that problem and it has not bothered me yet.

    I’ve built up a bit of a library of dialog ViewModels which I can easily extend. It’s way too much code to post here, but I’ll show you the highlights.

    Base ViewModel for Dialogs

    Each of my dialog windows has a ViewModel that inherits from DialogViewModelBase, which is similiar to my regular ViewModelBase in that it provides support for INotifyPropertyChanged etc. The interesting part is this public method, which I call from wherever to launch the Dialog:

    /// <summary>
    /// Creates window instance for this dialog viewmodel and displays it, getting the dialog result.
    /// </summary>
    public void ShowDialogWindow()
    {
        // This is a property of the DialogViewModelBase class - thus, each DialogViewModel holds a reference to its own DialogWindow:
        this.DialogWindow = new Dialogs.Views.DialogWindow();
        // Tell the DialogWindow to display this ViewModel:
        this.DialogWindow.DataContext = this;
        // Launch the Window, using a method of the Window baseclass, that only returns when the window is closed:
        this.DialogWindow.ShowDialog();
    }
    

    Window launched in the above method will close when its Window.DialogResult property is set. This is why the DialogWindow is a property of the DialogViewModelBase class – when the subclassing dialog ViewModel wants to close the dialog window, it simply sets the result:

    protected void CloseDialogWithResult(bool dialogWindowResult)
    {
        // Setting this property automatically closes the dialog window:
        this.DialogWindow.DialogResult = dialogWindowResult;
    }
    

    Host Window for Dialog Views

    The Dialogs.Views.DialogWindow class that the ShowDialogWindow method instantiates is defined in XAML and is a subclass of Window. It has two important features. The first is that it’s primary content element is simply a ContentControl that binds to the current context. This allows me to define different Views for different subclasses of DialogViewModelBase, and the DialogWindow will host the corresponding View based on the type of the context:

    <ContentControl Content="{Binding}" /> <!-- In reality this is inside a border etc but its simplified here for demonstration -->
    

    The second important feature of the DialogWindow XAML is that it defines which dialog Views go with which dialog ViewModels. Here is a sample:

    <Window.Resources>
        <!-- DEFAULT ViewModel-View TEMPLATES -->
    
        <DataTemplate DataType="{x:Type dialogs:YesNoMessageBoxDialogViewModel}">
            <views:MessageBoxView />
        </DataTemplate>
    
        <DataTemplate DataType="{x:Type dialogs:ErrorDialogViewModel}">
            <views:ErrorDialogView/>            
        </DataTemplate>
    
    </Window.Resources>
    

    What all this does, is that I can define dialogs as subclasses to DialogViewModelBase and implement a View for each, and then tell DialogWindow which View its ContentControl must show for which dialog ViewModel.

    Launching a Dialog and getting results

    Below is a sample from one of my application ViewModels, in which I launch a Dialog Window that allows the user to select an Asset Type for creation:

    public void CreateNewAsset()
    {
        // Instantiate desired Dialog ViewModel:
        Dialogs.NewAssetTypeSelectionDialogViewModel dialog = new Dialogs.NewAssetTypeSelectionDialogViewModel();
    
        // Launch Dialog by calling method on Dialog base class:
        dialog.ShowDialogWindow();
    
        // Execution will halt here until the Dialog window closes...
    
        // The user's selection is stored in a property on the dialog ViewModel, and can now be retrieved:
        CalculatorBase.AssetTypeEnum newAssetType = dialog.AssetType;
    
        switch (newAssetType)
        {
            // Do stuff based on user's selection...
        }
    }
    

    PS: I should really write a blog entry about this – when I do, I will post the link here, as the blog entry will probably have more complete code samples.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I've tracked down a weird MySQL problem to the two different ways I was
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I want to construct a data frame in an Rcpp function, but when I
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

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.