c# - Load ResourceDictionary item dynamically based on property value -


is possible load 1 of stackpanels based on string property inside viewmodel? if string mystackpanel1 appropiate stackpanel injected grid of mainwindow.

my resourcedictionary

<resourcedictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">       <stackpanel x:key="mystackpanel1" background="{binding color}">         // has content          </stackpanel>      <stackpanel x:key="mystackpanel2" background="{binding color}">         // has other content     </stackpanel> </resourcedictionary> 

my mainwindow:

<window x:class="wpfapp.mainwindow"         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">     <window.resources>         <resourcedictionary>             <resourcedictionary.mergeddictionaries>                 <resourcedictionary source="dictionary.xaml"/>             </resourcedictionary.mergeddictionaries>         </resourcedictionary>     </window.resources>      <grid>      </grid> </window> 

here idea of viewmodel:

public class viewmodel : inotifypropertychanged {   public event propertychangedeventhandler propertychanged;   public string stackpanelname { get; set; };   public string color { get; set; };     private void changedhandler(string propertytobechanged) {     } } 

you use contentcontrol contenttemplates bindings work should set content property of contentcontrol:

<window.resources>     <resourcedictionary>         <datatemplate x:key="myresource1" x:shared="false">             <stackpanel>                 <textblock background="{binding background}">hello world</textblock>             </stackpanel>         </datatemplate>          <!-- resource2 , on -->     </resourcedictionary> </window.resources>  <grid x:name="body">     <!-- "background" property of view model -->     <contentcontrol x:name="sample" content="{binding}" contenttemplate="{staticresource myresource1}"/> </grid> 

Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -