wpf - Set ContentTemplate for ListViewItem from Custom View -
i create custom view listview control.
public class customview: view{ //... public datatemplate itemtemplate { get; set; } //... } <customview x:key="custom"> <customview.itemtemplate> <datatemplate> </datatemplate> </customview.itemtemplate> </customview>
in style listviewitem
bind datatemplate
customview.itemtemplate
on contenttemplate
.
<style x:key="{componentresourcekey typeintargetassembly={x:type customview}, resourceid=customviewitem}" targettype="{x:type listviewitem}" basedon="{staticresource {x:type listboxitem}}"> <setter property="contenttemplate" value="{binding path=view.itemtemplate, relativesource={relativesource findancestor, ancestortype={x:type listview}}}" /> <setter property="template"> <setter.value> <controltemplate targettype="{x:type listboxitem}"> <border x:name="itemborder"> <contentpresenter /> </border> </setter.value> </setter> </style> <style x:key="{componentresourcekey typeintargetassembly={x:type customview}, resourceid=customview}" targettype="{x:type listview}" basedon="{staticresource {x:type listbox}}"> </style>
view works in output window in vs error:
cannot find source binding reference 'relativesource findancestor, ancestortype='system.windows.controls.listview', ancestorlevel='1''. bindingexpression:path=view.itemtemplate; dataitem=null; target element 'listviewitem' (name=''); target property 'contenttemplate' (type 'datatemplate')
public class customlistview : listview {
static customlistview() {
defaultstylekeyproperty.overridemetadata(typeof(customlistview), new frameworkpropertymetadata(typeof(customlistview)));
} protected override dependencyobject getcontainerforitemoverride() { return new customlistviewitem(); }
}
Comments
Post a Comment