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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:31:37+00:00 2026-05-28T20:31:37+00:00

This is a question that extends from the originally posted here: Link to loading-xaml

  • 0

This is a question that extends from the originally posted here:
Link to loading-xaml through runtime

I’m working on a WPF MVVM application that loads XAML content dynamically from an external source, very similar as the answer in the post above.
Here is what I got so far:

  1. My View declares an instance of the ViewModel as a resource and creates an instance of that ViewModel
  2. In my ViewModel constructor I’m loading a XamlString property coming from an external source (file or db..)
  3. In my view I have a button that user clicks after ViewModel finishes loading and in the click-event code-behind I’m deserializing the dynamically loaded XAML and add it to my grid.

My question is, how can I eliminate code-behind and automate the logic so the View can render the new xaml section dynamically right after the ViewModel is done getting the XAML content and initializing the string property?

Should I use some kind of Messaging Bus so the ViewModel notifies once the property has been set so the View can add the new content?

What troubles me is the fact that ViewModels do have a reference to Views and should not be in charge of generating UI elements.

Thanks in advance!

Edit:
Just to clarify: in my particular case I am not trying to bind a Business Object or Collection (Model) to a UI element (e.g. Grid) which obviously could be accomplished through templates and binding. My ViewModel is retrieving a whole XAML Form from an external source and setting it as a string property available to the View.

My question is: Who should be in charge of deserializing this XAML string property into a UI element and add it programmatically to the my grid once my Xaml string property in the VM is set?

This sounds to me more of like a View responsibility, not ViewModel. But the pattern as i understand it enforces to replace any code-behind logic with V-VM bindings.

  • 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-28T20:31:37+00:00Added an answer on May 28, 2026 at 8:31 pm

    I have a working solution now and I’d like to share it. Unfortunately I did not get rid of code-behind completely but it works as I expect it to. Here is how it works(simplified):

    I have my simplified ViewModel:

    public class MyViewModel : ViewModelBase
    {
       //This property implements INPC and triggers notification on Set
       public string XamlViewData {get;set;}
    
       public ViewModel()
       {
          GetXamlFormData();  
       }
    
       //Gets the XAML Form from an external source (e.g. Database, File System)
       public void GetXamlFormData()
       {
           //Set the Xaml String property
           XamlViewData = //Logic to get XAML string from external source
       }
    }
    

    Now my View:

    <UserControl.Resources>
    <ViewModel:MyViewModel x:Key="Model"></ViewModel:MyViewModel>
    </UserControl.Resources>
    <Grid DataContext="{StaticResource Model}">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <StackPanel>
        <!-- This is the Grid used as a Place Holder to populate the dynamic content!-->
        <Grid x:Name="content" Grid.Row="1" Margin="2"/>
        <!-- Then create a Hidden TextBlock bound to my XamlString property. Right after binding happens I will trigger an event handled in the code-behind -->
        <TextBlock Name="tb_XamlString" Text="{Binding Path=XamlViewData, Mode=TwoWay, UpdateSourceTrigger=LostFocus, NotifyOnValidationError=True, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}" Visibility="Hidden" Loaded="tb_XamlString_Loaded" />
        </StackPanel>
    </Grid>
    

    Basically I created a hidden TextBlock bound to my XAML String property in the ViewModel and I hooked its Loaded event to an event handler in the code behind of the View:

        private void tb_XamlString_Loaded(object sender, RoutedEventArgs routedEventArgs)
        {
            //First get the ViewModel from DataContext
            MyViewModel vm = content.DataContext as MyViewModel;
            FrameworkElement rootObject = XamlReader.Parse(vm.XamlViewData) as FrameworkElement;
            //Add the XAML portion to the Grid content to render the XAML form dynamically!
            content.Children.Add(rootObject);
        }
    

    This may not be the most elegant but gets the job done. Like some people say, in MVVM there are some cases like this where little code-behind code is needed. It doesn’t hurt and also part of this solution still uses the V-VM Binding principles when using the VM to retrieve and populate the XamlString property and exposing it to the View. If we would like to Unit Test the XAML parsing and loading functionality we could delegate it to a separate class.

    I hope someone finds this useful!

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

Sidebar

Related Questions

I know from this question that Firefox 3.0 and up stores its cookies in
I originally posted this question on the miglayout forum and after 534 views and
I'm quite ashemed to ask this question here because I'm sure that I'm missing
This question is tied in with this other question that I asked I have
I found this question that is discussing what I would like to do, but
I found this question that was very useful in learning the basics of the
I have a UIView subclass ( CustomView for purposes of this question) that has
I have this little question that's been on my mind for a while now.
This question comes from my experience with the following question: https://stackoverflow.com/questions/492748/new-responses-icon-on-so-crashes-ie7-closed In that question,
The very act of asking this question suggests that my approach to this problem

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.