ACour008
Topic Author
Posts: 2
Joined: 26 Jul 2026, 01:08

Question about working with ContentTemplates

26 Sep 2026, 17:56

Hello,

I am newer to MVVM than i previously thought (classic Dunning-Kruger effect). As such, I'd like some help wrapping my head around some concepts for what I am trying to achieve. What I am trying to do is to have switchable HUDs that belong to a ContentControl in my Root xaml. More specifically, hud designs that are in their own xaml file. For clarity, I'm working in Unity, but based on this post (viewtopic.php?p=19685&hilit=multiple+xaml#p19685), it was noted that one can "...'switch screen' by simply changing the Template used by the ContentControl's 'Content Template' Property".

I understand this & have created a couple test content templates which easily switch out in run-time design. That is fun, but how do I achieve this at run time, through code? Is this a regular binding where I create a C# property and invoke the PropertyChanged event? If so, what should Noesis's data structure look like for that? Because I have been unsuccessful at any template being rendered on screen (unless i've specifically set it in Noesis Studio). If no, what is the recommended MVVM approach to do this?

Thanks for your help & sorry if this Q has been asked before!
 
ACour008
Topic Author
Posts: 2
Joined: 26 Jul 2026, 01:08

Re: Question about working with ContentTemplates

27 Sep 2026, 17:04

Hello, just a quick update: I was able to find a solution by using data templates, enumerations, triggers and actions within Noesis Studio so that when I instantiate a new SubclassHudViewModel from my RootViewModel (or wherever its needed) on the C# side, the proper data template shows on screen.

Ie., My data structures are set up in Noesis Studio so I have a general HudViewModel & two 'sub' structures that are based on it, and I have the enum as HudType. There are two data triggers that have actions to set the corresponding data template to my root's ContentControl. On the C# side, I mirror the Noesis data structures and my RootViewModel has a HudViewModel property that invokes the property changed event whenever it changes (say like when a new game mode starts and does `root.Hud = new SubclassViewModel()`.

Its pretty cool but having to use enums for such things can get unwieldy if I ever had to extend my project. Fortunately, I dont think it will get super complex beyond what i've set up, but if anyone has any other recommendations, I'd love to hear them.
 
User avatar
sfernandez
Site Admin
Posts: 3363
Joined: 22 Dec 2011, 19:20

Re: Question about working with ContentTemplates

29 Sep 2026, 12:25

Hello,

Your approach is one way to achieve it, by using a combination of triggers to explicitly set the corresponding DataTemplate in your ContentControl.

Another alternative is to rely on the automatic template resolution by type. Imagine you have 2 HUD view models, HUD_A and HUD_B. For each one of them you define a DataTemplate, and store it in the dictionary as the default for that type (in xaml it will look something like: <DataTemplate DataType="local:HUD_A">). Then your view model will expose a property for the ActiveHUD, which will be updated in code and notify the change properly through the INotifyPropertyChanged interface. And finally you will just bind the ContentControl.Content property to the ActiveHUD:
<ContentControl Content="{Binding ActiveHUD}"/>
Have you tried that? This is how our LayeredUI sample works, take a look at: https://github.com/Noesis/Tutorials/tre ... edUI/Unity

Who is online

Users browsing this forum: Baidu [Spider], Google [Bot] and 4 guests