wpf - Binding a DataTemplate to ContentControl with ContentTemplateSelector -
i have 3 datatemplate in window.resources, typed 3 different classes:
<datatemplate x:key="mytemplatea" datatype="{x:type local:sampleclassa}"> <grid> <textblock text="{binding path=sampleproperty1}"></textblock> <textblock text="{binding path=sampleproperty2}"></textblock> <textblock text="{binding path=samplepropertya}"></textblock> </grid> </datatemplate> <datatemplate x:key="mytemplateb" datatype="{x:type local:sampleclassb}"> <grid> <textblock text="{binding path=sampleproperty1}"></textblock> <textblock text="{binding path=sampleproperty2}"></textblock> <textblock text="{binding path=samplepropertyb}"></textblock> </grid> </datatemplate> <datatemplate x:key="mytemplatebase" datatype="{x:type local:sampleclass}"> <grid> <textblock text="{binding path=sampleproperty1}"></textblock> <textblock text="{binding path=sampleproperty2}"></textblock> </grid> </datatemplate>
and little datatemplateselector:
<local:sampledatatemplateselector x:key="mybindpropertytemplateselector" templatea="{staticresource mytemplatea}" templateb="{staticresource mytemplateb}" sampleclassbasetemplate="{staticresource mytemplatebase}" />
obviously there class datatemplateselector, called sampledatatemplateselector
public class sampledatatemplateselector inherits datatemplateselector public property templatea datatemplate public property templateb datatemplate public property sampleclassbasetemplate datatemplate public overrides function selecttemplate(item object, container dependencyobject) datatemplate if item nothing return sampleclassbasetemplate if item.gettype.equals(gettype(sampleclassa)) return templatea end if if item.gettype.equals(gettype(sampleclassb)) return templateb end if return sampleclassbasetemplate end function end class
datatemplateselector must discriminate 3 classes: sampleclass , 2 derived classes sampleclassa, sampleclassb.
public class sampleclass public property sampleproperty1 string = "class sampleclass property1" public property sampleproperty2 string = "class sampleclass property2" end class public class sampleclassa inherits sampleclass public property samplepropertya string = "class sampleclassa" end class public class sampleclassb inherits sampleclass public property samplepropertyb string = "class sampleclassb" end class
in window xaml contentcontrol set follow:
<contentcontrol content="{binding bindproperty}" contenttemplateselector="{staticresource mybindpropertytemplateselector}" />
where bindproperty property defined on window code behind:
dim mybindproperty sampleclass public readonly property bindproperty sampleclass return mybindproperty end end property
after all, in window load event there bindproperty initialization
private sub window_loaded(sender system.object, e system.windows.routedeventargs) handles mybase.loaded mybindproperty = new sampleclassa() end sub
there's no compile errors. in execution, item argument on sampledatatemplateselector's selecttemplate routine ever nothing, can't choose right template... , shows nothing. doing wrong?
i recreated project , work if add datacontext = me window_loaded. changed templates use stackpanel instead of grid (so text blocks didn't overlap each other). here complete, working version:
mainwindow.xaml
<window x:class="mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:wpfapplication20" loaded="window_loaded" title="mainwindow" height="350" width="525"> <window.resources> <datatemplate x:key="mytemplatea" datatype="{x:type local:sampleclassa}"> <stackpanel> <textblock text="{binding path=sampleproperty1}"></textblock> <textblock text="{binding path=sampleproperty2}"></textblock> <textblock text="{binding path=samplepropertya}"></textblock> </stackpanel> </datatemplate> <datatemplate x:key="mytemplateb" datatype="{x:type local:sampleclassb}"> <stackpanel> <textblock text="{binding path=sampleproperty1}"></textblock> <textblock text="{binding path=sampleproperty2}"></textblock> <textblock text="{binding path=samplepropertyb}"></textblock> </stackpanel> </datatemplate> <datatemplate x:key="mytemplatebase" datatype="{x:type local:sampleclass}"> <stackpanel> <textblock text="{binding path=sampleproperty1}"></textblock> <textblock text="{binding path=sampleproperty2}"></textblock> </stackpanel> </datatemplate> <local:sampledatatemplateselector x:key="mybindpropertytemplateselector" templatea="{staticresource mytemplatea}" templateb="{staticresource mytemplateb}" sampleclassbasetemplate="{staticresource mytemplatebase}" /> </window.resources> <grid> <contentcontrol content="{binding bindproperty}" contenttemplateselector="{staticresource mybindpropertytemplateselector}" /> </grid> </window>
mainwindow.xaml.vb
class mainwindow dim mybindproperty sampleclass public readonly property bindproperty sampleclass return mybindproperty end end property private sub window_loaded(sender object, e routedeventargs) mybindproperty = new sampleclassa() datacontext = me end sub end class public class sampleclass public property sampleproperty1 string = "class sampleclass property1" public property sampleproperty2 string = "class sampleclass property2" end class public class sampleclassa inherits sampleclass public property samplepropertya string = "class sampleclassa" end class public class sampleclassb inherits sampleclass public property samplepropertyb string = "class sampleclassb" end class public class sampledatatemplateselector inherits datatemplateselector public property templatea datatemplate public property templateb datatemplate public property sampleclassbasetemplate datatemplate public overrides function selecttemplate(item object, container dependencyobject) datatemplate if item nothing return sampleclassbasetemplate if item.gettype.equals(gettype(sampleclassa)) return templatea end if if item.gettype.equals(gettype(sampleclassb)) return templateb end if return sampleclassbasetemplate end function end class
Comments
Post a Comment