c# - Creating a custom control in WPF -
having custom control's content defined this:
<dockpanel> <listview x:name="tabcontrolmenu" dockpanel.dock="top" scrollviewer.verticalscrollbarvisibility="disabled" itemssource="{binding relativesource={relativesource findancestor, ancestortype={x:type usercontrol}}, path=items}"> <listview.resources> <style basedon="{staticresource listviewstyle}" targettype="{x:type listview}" /> <style basedon="{staticresource listviewitemstyle}" targettype="{x:type listviewitem}" /> </listview.resources> <listview.itemspanel> <itemspaneltemplate> <stackpanel orientation="horizontal"></stackpanel> </itemspaneltemplate> </listview.itemspanel> </listview> <border borderbrush="black" borderthickness="1,0,0,0" dockpanel.dock="bottom"> <contentpresenter datacontext="{binding selecteditem}" content="{binding body, elementname=thiscontrol}" /> </border>
public partial class mytabcontrol : usercontrol { #region static properties public static readonly dependencyproperty bodyproperty = dependencyproperty.register( "body", typeof(object), typeof(mytabcontrol ), new uipropertymetadata(null)); public static readonly dependencyproperty itemsproperty = dependencyproperty.register( "items", typeof(observablecollection<object>), typeof(mytabcontrol), new uipropertymetadata(null)); public static readonly dependencyproperty selecteditemproperty = dependencyproperty.register( "selecteditem", typeof(object), typeof(mytabcontrol), new uipropertymetadata(null)); #endregion #region public properties public object body { { return getvalue(bodyproperty); } set { setvalue(bodyproperty, value); } } public observablecollection<object> items { { return (observablecollection<object>)getvalue(itemsproperty); } set { setvalue(itemsproperty, value); } } public object selecteditem { { return getvalue(selecteditemproperty); } set { setvalue(selecteditemproperty, value); } } public observablecollection<listviewitem> listviewitems { get; set; } #endregion public mytabcontrol() { initializecomponent(); listviewitems = new observablecollection<listviewitem>(); } }
and using this:
<controls:mytabcontrol margin="5,0,0,0" items="{binding items}"> <controls:mytabcontrol.body> <contentcontrol content=" binding goes here???? "> <!--<contentcontrol content="{binding}">--> <contentcontrol.resources> <datatemplate datatype="{x:type viewmodels:projectviewmodel}"> <partials:projectview datacontext="{binding path=.}" /> </datatemplate> <datatemplate datatype="{x:type viewmodels:testsuiteviewmodel}"> <partials:testsuiteview datacontext="{binding path=.}" /> </datatemplate> <datatemplate datatype="{x:type viewmodels:stepviewmodel}"> <partials:stepview datacontext="{binding path=.}" /> </datatemplate> </contentcontrol.resources> </contentcontrol> </controls:mytabcontrol.body> </controls:mytabcontrol>
how can set contentcontrol
's content
binding point defined inside mytabcontrol
?
probably people ask why creating custom tab control in first place - answer is: because have not standard - grouping tabs, coloring, dragging them around etc...
edit:
my goal here mytabcontrol
behave standard wpf tabcontrol
- it's items
or itemssource
should bound collection , contents of tab should bound whatever selected. achieve i'm binding listview
items
collection on control code-behind , trying bind contentpresenter
's datacontext
whatever selected (it set in code behind). problem if use mytabcontrol.body
somewhere in application don't know how bind contentcontrol.content
property it's data custom control.
if understand correctly, need make usercontrol
it's own datacontext
. problem is, want controls inside usercontrol
see usercontrol
datacontext
, need usercontrol
see other datacontext
. can setting datacontext
of root layout element, think dockpanel
. this:
<usercontrol ... x:name="thiscontrol ... > <dockpanel datacontext="{binding elementname=thiscontrol}"> ... </listview> ... </dockpanel> </usercontrol>
http://www.codeproject.com/articles/325911/a-simple-pattern-for-creating-re-useable-usercontr proper explanation of trying say.
edit: think might need, datacontext
contentpresenter
:
<dockpanel datacontext="{binding elementname=thiscontrol}"> ... <listview itemssource="{binding items}" ... /> <border ... > <contentpresenter datacontext="{binding selecteditem}" ... /> </border>
or relativesource
contentpresenter
have itemssource
in listview
:
<dockpanel> <listview ... itemssource="{binding relativesource= {relativesource findancestor, ancestortype={x:type usercontrol}}, path=items}"> ... </listview> <border ...> <contentpresenter datacontext="{binding relativesource= {relativesource findancestor, ancestortype={x:type usercontrol}}, path=datacontext.selecteditem}" ... /> </border>
2nd edit:
it looks though trying set properties of mytabcontrol
@ xaml level, think should setting dp's, xaml binds to. this? :
<controls:mytabcontrol ... body="{binding thebody}" items="{binding theitems}"> selecteditem="{binding theselecteditem}" />
Comments
Post a Comment