c# - WPF Custom Control with Item/Data Templates -
i know how create custom user control in wpf how can make can provide itemtemplate?
i have user control mixture of several other wpf controls, 1 of them being listbox. i'd let user of control specify content of list box i'm not sure how pass information through.
edit: accepted answer works following correction:
<usercontrol x:class="wpfapplication6.mycontrol"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     xmlns:src="clr-namespace:wpfapplication6">     <listbox itemtemplate="{binding relativesource={relativesource findancestor, ancestortype={x:type src:mycontrol}}, path=itemssource}" /> </usercontrol>      
you want add dependencyproperty control. xaml different if deriving usercontrol or control.
public partial class mycontrol : usercontrol {     public mycontrol()     {         initializecomponent();     }      public static readonly dependencyproperty itemtemplateproperty =         dependencyproperty.register("itemtemplate", typeof(datatemplate), typeof(mycontrol), new uipropertymetadata(null));     public datatemplate itemtemplate     {         { return (datatemplate) getvalue(itemtemplateproperty); }         set { setvalue(itemtemplateproperty, value); }     } }   here xaml usercontrol.
<usercontrol x:class="wpfapplication6.mycontrol"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     xmlns:src="clr-namespace:wpfapplication6">     <listbox itemtemplate="{binding itemtemplate, relativesource={relativesource findancestor, ancestortype={x:type src:mycontrol}}}" /> </usercontrol>   here xaml control:
<style targettype="{x:type src:mycontrol}">     <setter property="template">         <setter.value>             <controltemplate targettype="{x:type src:mycontrol}">                 <border background="{templatebinding background}"                         borderbrush="{templatebinding borderbrush}"                         borderthickness="{templatebinding borderthickness}">                      <listbox itemtemplate="{templatebinding itemtemplate}" />                 </border>             </controltemplate>         </setter.value>     </setter> </style>      
Comments
Post a Comment