c# - How can I make WPF instantiate a custom control in my view, using another custom control base class in my XAML? -


i have listview 5 columns:

  <listview x:name="fieldlist" itemssource="{binding monitorfield}" selecteditem="{binding field}" margin="33,22,87,209" grid.column="1" grid.rowspan="2">             <listview.view>                 <gridview>                     <gridviewcolumn width="140" header="field name">                         <gridviewcolumn.celltemplate>                             <datatemplate>                                 <textbox width="127" text="{binding id}" height="32" fontsize="16" isreadonly="false" background="transparent" borderthickness="0" textwrapping="wrap"/>                             </datatemplate>                         </gridviewcolumn.celltemplate>                     </gridviewcolumn>                      <gridviewcolumn width="140" header="file type" >                         <gridviewcolumn.celltemplate>                             <datatemplate>                                 <combobox width="127" itemssource="{binding resourcetypevalues}"  selecteditem="{binding resourcetypetoload}" height="24" fontsize="16" background="transparent" borderthickness="0" />                             </datatemplate>                         </gridviewcolumn.celltemplate>                     </gridviewcolumn>                      <gridviewcolumn width="140" header="path" >                         <gridviewcolumn.celltemplate>                             <datatemplate>                                 <mynamespace:pathcontrol width="127" text="{binding resourcepathtoload, mode=twoway}"  height="32" fontsize="16" background="transparent" textwrapping="wrap">                                     <mynamespace:pathcontrol.inputbindings>                                         <mousebinding mouseaction="leftdoubleclick" command="{binding browsefilecommand}" />                                     </mynamespace:pathcontrol.inputbindings>                                 </mynamespace:pathcontrol>                             </datatemplate>                         </gridviewcolumn.celltemplate>                     </gridviewcolumn>                 </gridview>             </listview.view> 

here custom control pathcontrol et testcontrol inherit pathcontrol

public class pathcontrol : textbox, ipathcontrol {     static pathcontrol()     {         //defaultstylekeyproperty.overridemetadata(typeof(pathcontrol), new frameworkpropertymetadata(typeof(pathcontrol)));     } }  public class testcontrol : pathcontrol {     static testcontrol()     {      } } 

what making wpf instantiate custom control depending on combobox defining before pathcontrol.

for example, if select "txt" in combobox, want create txtcontrol inherit pathcontrol.

the mouse binding call different method depending on custom control instantiate.

is possible ? there way implement ?

first define resources 2 datatemplates want like:

<datatemplate x:key="case1">  <c:pathcontrol /> </datatemplate> 

then other

<datatemplate x:key="case2">  <c:testcontrol /> </datatemplate> 

now create datatemplateselector

public class selectiontemplateselector : datatemplateselector { public datatemplate case1template { get; set; } public datatemplate case2template { get; set; }  public override datatemplate selecttemplate(object item,  dependencyobject container) {  if( //get binding need) return case1template ; else return case2template ; } } 

now add resource:

 <c:selectiontemplateselector      imagetemplate="{staticresource case1}"      stringtemplate="{staticresource case2}"      x:key="selectiontemplateselector " /> 

and instead adding datatemplate add

itemtemplateselector="{staticresource selectiontemplateselector }" 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -