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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:27:13+00:00 2026-06-07T17:27:13+00:00

Ok, this is a little hard for me to explain, so Im writing a

  • 0

Ok, this is a little hard for me to explain, so Im writing a program using MVVM model with WPF.

I have a grid on my main UserControl which requires the children to be updated (by updated I mean created and destroyed) dynamically through code (as data is changed by the ViewModel I want the children of the Grid to be created and destroyed as needed).

Im pretty sure you cant dynamically create and destroy UIElements using only XAML, so I have update functions in my view (these update functions ONLY update the UI itself they do not change anything in the ViewModel, so therefore im not breaking MVVM because the code in the view only alters the UI based on the data in the ViewModel, so thats ok right?).

Right now I am assigning the PropertyChanged event of my ViewModel within the View and catching that through code and firing the Update events, so my event in my View class looks like this:

        private void ViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "BoardWidth" || e.PropertyName == "BoardHeight")
                RecreateBoard();

            if (e.PropertyName == "Coordinates")
                UpdateCoordinates();

            if (e.PropertyName == "TilesOnly")
            {
                UpdateBoardBlocks();
                UpdateTiles();
            }

            if (e.PropertyName == "BoardBlockViewModels")
                UpdateBoardBlocks();

            if (e.PropertyName == "TileViewModels")
                UpdateTiles();

            if (e.PropertyName == "EntryPoint" | e.PropertyName == "CursorViewModel")
                UpdateEntryPoint();
        }

The update events create / destroy children within the main grid of the view using code based on the data in the ViewModel. This current implementation works well.

But, I would like to implement the event handler through XAML. So my question is how do I implement the event handler code above in XAML, I cant find any way of executing a Method within the view when a datacontext property changes. I have looked into DataTriggers but they dont contain the ability to execute the methods. Also the DataTrigger would need to fire when the property changes, not when it is set to a specific value.

I do not wish to push the updates through the ViewModel because this is STICTLY a View thing it has nothing to do with the viewmodel itself. eg. A different implementation of the View may not need to update in these circumstances. Even if I was to pus it through the ViewModel, the view itself would still have the same issue of how to execute the methods.

Thankyou for your help.

  • 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-07T17:27:14+00:00Added an answer on June 7, 2026 at 5:27 pm

    I think the way you approach may be wrong if you need that much imperative code in your view. In most cases you just bind data to lists and fields etc. You rarely need to create and remove elements, you either have controls autogenerate them (e.g. ItemsControls) or you reuse the existing instances.

    If you do need to do such dynamic things it might be best to encapsulate that logic in UserControls or custom controls, just offering properties that can be bound (internally you can handle the dependency property’s value changed event to do any updates).

    What is left in the view should be bindings to those controls.

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

Sidebar

Related Questions

This is a little hard to explain.. I am using tomcat 6.0 to test
I think this is a little hard to explain. Basically, I have an android
This is a little hard to explain, but I have an HTML table, full
This is a little hard to explain but I'll try my best. I have
Greetings Guru's, This is a little hard to explain, but I'll give it a
This a little hard for me to explain, but I'll try my best. I'm
This may be a little hard to describe since I don't have a sample.
its a little bit hard to understand. in the header.php i have this code:
have written this little class, which generates a UUID every time an object of
This one is a little hard for me to explain correctly so please bear

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.