c# - In WPF is it possible to define a ControlTemplate for a UserControl inside the UserControl itself (without getting warnings/errors in VS)? -


i have usercontrol want show different control templates in parts of window. want keep these templates inside usercontrol (for better maintaining). here is:

<usercontrol x:class="pruebasdewpf.myusercontrol"          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"          xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"           xmlns:d="http://schemas.microsoft.com/expression/blend/2008"           xmlns:local="clr-namespace:pruebasdewpf"          mc:ignorable="d"           d:designheight="300" d:designwidth="300"> <usercontrol.resources>     <controltemplate x:key="usercontroltemplate1" targettype="local:myusercontrol">         <grid>             <rectangle fill="red"></rectangle>         </grid>     </controltemplate>     <controltemplate x:key="usercontroltemplate2" targettype="local:myusercontrol">         <grid>             <rectangle fill="blue"></rectangle>         </grid>     </controltemplate> </usercontrol.resources> <grid>  </grid> 

now when use it:

<window x:class="pruebasdewpf.mainwindow"         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"         xmlns:local="clr-namespace:pruebasdewpf"         mc:ignorable="d"         title="mainwindow" height="350" width="525">     <grid>         <local:myusercontrol template="{staticresource usercontroltemplate1}"></local:myusercontrol>     </grid> </window> 

i error in visual studio saying resource not found , control doesn't show up. if change template dynamicresource, same message warning control shows. anyway, program runs fine. how keep usercontrol , templates without getting these annoying warnings/errors? need specific resourcedictionary (another file) that?

the pattern you're trying use isn't suited resources because of way loaded: top down tree. switching between internally defined options more appropriate define property on usercontrol (dependencyproperty if want bind it) indicate of available templates should used. string, number, or (probably nicest option) enum listing available options. inside usercontrol can switch internally defined template used based on value. basic method used various framework controls - example slider.orientation.


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -