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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:38:46+00:00 2026-06-13T22:38:46+00:00

Question on the MVVM pattern where I think I have it wrong. When a

  • 0

Question on the MVVM pattern where I think I have it wrong.

When a touchdown event occurs in a view I want to popup a message i.e.:

private void marker_TouchDown(MessageObject msgData)
{
    CustomMessageControl message = new CustomMessageControl() {Width = 610, Height = 332};
    CustomMessageViewModel messageVM = new CustomMessageViewModel(msgData);
    message.DataContext = messageVM;
    //Add to canvas
}

My viewmodel:

public class CustomMessageViewModel
{
    public MessageObject message { get; set; }

    public CustomMessageViewModel(MessageObject message)
    {
        this.MessageObject = message;
    }
}

This works but doesn’t feel right. Is this an acceptable way to populate the view model?

  • 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-13T22:38:47+00:00Added an answer on June 13, 2026 at 10:38 pm

    I believe that you are violating MVVM in creating the control in the view model. This isn’t testable, your view model has to create the control now and that shouldn’t be a requirement for testing (this emphasizes the lack of the separation of concerns between the UI and the view model).

    Instead of creating the control, it is completely acceptable for your view model to fire an event of it’s own. In this event, you’d pass the view model that you want the dialog/overlay/control to bind to, something like this:

    public class CustomMessageControlEventArgs : EventArgs
    {
        public CustomMessageViewModel CustomMessageViewModel { get; set; }
    }
    
    public event EventHandler<CustomMessageControlEventArgs> 
        ShowCustomMessageControl;
    
    private void marker_TouchDown(MessageObject msgData)
    {
        // Create the view model.
        var message = ...;
    
        // Get the events.
        var events = ShowCustomMessageControl;
    
        // Fire.
        if (events != null) events(this, 
            new CustomMessageControlEventArgs {
                MessageObject = new CustomMessageViewModel(msgData)
            });
    }
    

    Then, in your UI code, you would bind to the event and then show the appropriate user interface for that event.

    Remember, MVVM isn’t strictly about being able to declare everything in XAML or binding data to the UI through just data bindings, it’s about proper separation of code.

    You want to separate the what of what is displayed (the view model) from the how of what is displayed (the UI); in firing an event, you’re maintaining that separation of concerns.

    Yes, you’ll have to write some code behind (or you could do it through property notification changes, but it’s uglier, frankly), but it maintains the separation and allows for easy testability without having to bring in any user interface elements.

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

Sidebar

Related Questions

I am using the MVVM pattern, I have a view which creates a new
when using mvvm pattern, when we have lists we use ObservableCollection<>. my question is
I have a question regarding WPF UserControls and MVVM Pattern, I need to expose
I am using MVVM pattern to implement a WPF App. My question is, I
The application I'm currently writing is using MVVM with the ViewModel-first pattern. I have
I have wholly adopted the MVVM pattern for our silverlight app. However, some of
Well this question relates to MVVM pattern and i could good and fast answers
Okay i'm trying to understand WPF and the popular MVVM Pattern. Now i have
Question Is it possible to stop a Message Driven Bean (programmatically), so that it
QUESTION: What am I missing or doing wrong? I'm trying to migrate fully functional

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.