c# - Conditional Resources Creation WPF XAML Design / Run time -
following first question on wpf cascading binding,
i remarked had more resources desired defined in both mainwindow , usercontrols:
this seen in snoop
- the mainwindow xaml has "instance" of mainwindow viewmodel paints 2 usercontrol in green , blue
- additionnaly there 2 instances of usercontrol viewmodels each of 2 usercontrols paint inner circle in red
- my first intention in usercontrol able see "live" how control ( painted red distinguish "model" green , blue circles in main window)
- my resources cheap create, no matter if have 2 resources still living in application, anticipate time resources more expensive...
finally question :
how can conditionnaly let resources created in "low level" control ( can have preview in view of control ) but prevent creation when running full application ( or view of main window ) because bind resources of upper level.
many in advance.
best regards.
ngi
i prepared simplified way out displaying 1 data during design time , latter during run time. find useful , adjust case.
xaml:
<window.resources> <datatemplate x:key="designmodedatatemplate"> <textblock text="design mode"/> </datatemplate> <datatemplate x:key="runtimedatatemplate"> <textblock text="run time"/> </datatemplate> <local:isindesignmodeconverter x:key="isindesignmodeconverter"/> </window.resources> <contentcontrol> <contentcontrol.contenttemplate> <multibinding converter="{staticresource isindesignmodeconverter}"> <binding source="{staticresource designmodedatatemplate}"/> <binding source="{staticresource runtimedatatemplate}"/> </multibinding> </contentcontrol.contenttemplate> </contentcontrol>
converter:
class isindesignmodeconverter : imultivalueconverter { public object convert(object[] values, type targettype, object parameter, system.globalization.cultureinfo culture) { if ((bool)(designerproperties.isindesignmodeproperty.getmetadata(typeof(dependencyobject)).defaultvalue)) return values[0]; return values[1]; } public object[] convertback(object value, type[] targettypes, object parameter, system.globalization.cultureinfo culture) { throw new notimplementedexception(); } }
as result display design mode text during design time , run time when running. in case instead of textblock can insert defined resources.
Comments
Post a Comment