c# - How to dynamically change a WPF control's template using a checkbox? -


i have error dialog (shown simplified below).

i display report object in contentcontrol have defined template simpleerrortemplate.

there checkbox on window use change template to/from detailederrortemplate. best way achieve this?

<window x:class="core.errordialog"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">     <window.resources>          <controltemplate x:key="simpleerrortemplate">             <textbox margin="10,10,10,5" textwrapping="wrap" verticalscrollbarvisibility="auto" text="{binding message}" />         </controltemplate>          <controltemplate x:key="detailederrortemplate">             <textbox margin="10,10,10,5" textwrapping="wrap" verticalscrollbarvisibility="auto" text="{binding message}" />             <textbox margin="10,10,10,5" textwrapping="wrap" verticalscrollbarvisibility="auto" text="{binding details}" />             <textbox margin="10,10,10,5" textwrapping="wrap" verticalscrollbarvisibility="auto" text="{binding stacktrace}" />         </controltemplate>      </window.resources>     <grid>         <grid.rowdefinitions>             <rowdefinition height="*" />                      <rowdefinition height="50" />         </grid.rowdefinitions>          <contentcontrol grid.row="0" template="{staticresource simpleerrortemplate}" datacontext="{binding report}"/>          <checkbox margin="10,0,0,0" grid.row="1" x:name="chkshowdetails">show details</checkbox>                 </grid> </window> 

you can use datatrigger in contentcontrol style bind ischecked property of chkshowdetails checkbox

<contentcontrol grid.row="0" datacontext="{binding report}">     <contentcontrol.style>         <style targettype="contentcontrol">             <setter property="template"                     value="{staticresource simpleerrortemplate}"/>             <style.triggers>                 <datatrigger binding="{binding elementname=chkshowdetails,                                                path=ischecked}"                              value="true">                     <setter property="template"                             value="{staticresource detailederrortemplate}"/>                 </datatrigger>             </style.triggers>         </style>     </contentcontrol.style> </contentcontrol> 

update

complete xaml example, paste , try :)

<window.resources>     <controltemplate x:key="simpleerrortemplate">         <textbox margin="10,10,10,5" textwrapping="wrap" verticalscrollbarvisibility="auto" text="t1" />     </controltemplate>     <controltemplate x:key="detailederrortemplate">         <stackpanel>             <textbox margin="10,10,10,5" textwrapping="wrap" verticalscrollbarvisibility="auto" text="t2" />             <textbox margin="10,10,10,5" textwrapping="wrap" verticalscrollbarvisibility="auto" text="t3" />             <textbox margin="10,10,10,5" textwrapping="wrap" verticalscrollbarvisibility="auto" text="t4" />         </stackpanel>     </controltemplate> </window.resources> <grid>     <grid.rowdefinitions>         <rowdefinition height="*" />         <rowdefinition height="50" />     </grid.rowdefinitions>     <contentcontrol grid.row="0" datacontext="{binding report}">         <contentcontrol.style>             <style targettype="contentcontrol">                 <setter property="template"                         value="{staticresource simpleerrortemplate}"/>                 <style.triggers>                     <datatrigger binding="{binding elementname=chkshowdetails,                                                    path=ischecked}"                                  value="true">                         <setter property="template"                                 value="{staticresource detailederrortemplate}"/>                     </datatrigger>                 </style.triggers>             </style>         </contentcontrol.style>     </contentcontrol>     <checkbox margin="10,0,0,0" grid.row="1" x:name="chkshowdetails">show details</checkbox> </grid> 

Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -