wpf - XAML Binding issue, Parent-Child, with two ListBoxes -
pretty fresh in xaml, need help. little lost bindings. because think issue on "parent-child" relationship, make short version of code. maybe helps somewhere:)
problem: until use 1 listbox in code, need 2 listbox.
until bind so:
<datatrigger binding="{binding isselected, relativesource={relativesource findancestor, ancestortype={x:type listboxitem}}}" value="true">
how bind selected name? because have 2 different listbox. 1 have name "lstbox" "sldbox".
my short code:
<grid> <grid.resources> <datatemplate> <canvas> <thumb> <thumb.template> <controltemplate> <canvas> </canvas> <controltemplate.triggers> <datatrigger binding="{binding isselected, relativesource={relativesource findancestor, ancestortype={x:type listboxitem}}}" value="true"> <setter targetname="ellipse" property="fill" value="red"/> </datatrigger> </controltemplate.triggers> </controltemplate> </thumb.template> </thumb> </canvas> </datatemplate> </grid.resources> <grid> <listbox x:name=lstbox> <listbox.template> <controltemplate> <border> <border.background> </border.background> <scrollviewer> </scrollviewer> </border> </controltemplate> </listbox.template> <listbox.itemssource> <staticresource resourcekey="diagramcol"/> </listbox.itemssource> <listbox.itemspanel> <itemspaneltemplate> <canvas> </canvas> </itemspaneltemplate> </listbox.itemspanel> <listbox.itemcontainerstyle> <style> <setter> <setter.value> <controltemplate> <controltemplate.triggers> <triggers> </triggers> <datatrigger> </datatrigger> </controltemplate.triggers> </controltemplate> </setter.value> </setter> </style> </listbox.itemcontainerstyle> <listbox> <listbox x:name=sldbox> <listbox.template> <controltemplate> <border> <border.background> </border.background> <scrollviewer> </scrollviewer> </border> </controltemplate> </listbox.template> <listbox.itemssource> <staticresource resourcekey="diagramcol"/> </listbox.itemssource> <listbox.itemspanel> <itemspaneltemplate> <canvas> </canvas> </itemspaneltemplate> </listbox.itemspanel> <listbox.itemcontainerstyle> <style> <setter> <setter.value> <controltemplate> <controltemplate.triggers> <triggers> </triggers> <datatrigger> </datatrigger> </controltemplate.triggers> </controltemplate> </setter.value> </setter> </style> </listbox.itemcontainerstyle> <listbox> </grid>
my long code:
<grid> <grid.resources> <booleantovisibilityconverter x:key="booltovisibilityconverter"/> <compositecollection x:key="diagramcol"> <collectioncontainer collection="{binding datacontext.diagramconnectors, source={x:reference diagramview}}"/> <collectioncontainer collection="{binding datacontext.diagramnodes, source={x:reference diagramview}}"/> </compositecollection> <compositecollection x:key="slidercol"> <collectioncontainer collection="{binding datacontext.slidernodes, source={x:reference diagramview}}"/> </compositecollection> <!--nodes--> <datatemplate datatype="{x:type local:diagramnode}"> <canvas name="dragtarget"> <thumb dragdelta="thumb_drag" dragstarted="thumb_dragstarted" dragcompleted="thumb_dragcompleted"> <thumb.template> <controltemplate targettype="thumb"> <canvas margin="-10,-10,10,10"> <ellipse height="17" width="17" stroke="black" strokethickness="1" fill="yellow" x:name="ellipse"/> <textblock canvas.top="-20" canvas.left="-40" width="100" textalignment="center" text="{binding name}" fontweight="bold" ishittestvisible="false" visibility="{binding datacontext.shownames, relativesource={relativesource findancestor, ancestortype=window}, converter={staticresource booltovisibilityconverter}}"/> <textblock canvas.left="30" canvas.top="10" text="{binding x, stringformat='{}x = {0}'}" ishittestvisible="false" visibility="visible" x:name="xtext"/> <textblock canvas.left="30" canvas.top="25" text="{binding xmeaning}" ishittestvisible="false" visibility="visible" x:name="timetext"/> <textblock canvas.left="30" canvas.top="40" text="{binding y, stringformat='{}y = {0}'}" ishittestvisible="false" visibility="visible" x:name="ytext"/> </canvas> <controltemplate.triggers> <datatrigger binding="{binding isselected, relativesource={relativesource findancestor, ancestortype={x:type listboxitem}}}" value="true"> <setter targetname="ellipse" property="fill" value="red"/> </datatrigger> <trigger property="isdragging" value="true"> <setter targetname="ellipse" property="fill" value="green"/> </trigger> <datatrigger binding="{binding datacontext.showallcoordinates, relativesource={relativesource findancestor, ancestortype=window}}" value="true"> <setter targetname="xtext" property="visibility" value="visible"/> <setter targetname="ytext" property="visibility" value="visible"/> </datatrigger> <datatrigger binding="{binding }" value="true"> <setter targetname="ellipse" property="strokethickness" value="2"/> <setter targetname="ellipse" property="stroke" value="red"/> <setter targetname="ellipse" property="fill" value="white"/> </datatrigger> <multidatatrigger> <multidatatrigger.conditions> <condition binding="{binding isselected, relativesource={relativesource findancestor, ancestortype={x:type listboxitem}}}" value="true"/> <condition binding="{binding datacontext.showcurrentcoordinates, relativesource={relativesource findancestor, ancestortype=window}}" value="true"/> </multidatatrigger.conditions> <setter targetname="xtext" property="visibility" value="visible"/> <setter targetname="ytext" property="visibility" value="visible"/> </multidatatrigger> </controltemplate.triggers> </controltemplate> </thumb.template> </thumb> </canvas> </datatemplate> <!--sliders--> <datatemplate datatype="{x:type local:slidernode}"> <canvas name="dragslider"> <thumb dragdelta="thumb_drag" dragstarted="thumb_dragstarted" dragcompleted="thumb_dragcompleted"> <thumb.template> <controltemplate targettype="thumb"> <canvas margin="-10,-10,10,10"> <rectangle height="22" width="5" stroke="black" strokethickness="1" fill="yellow" x:name="rectangle"/> <!--<ellipse height="17" width="17" stroke="black" strokethickness="1" fill="yellow" x:name="ellipse"/>--> <textblock canvas.top="-20" canvas.left="-40" width="100" textalignment="center" text="{binding name}" fontweight="bold" ishittestvisible="false" visibility="{binding datacontext.shownames, relativesource={relativesource findancestor, ancestortype=window}, converter={staticresource booltovisibilityconverter}}"/> <textblock canvas.left="30" canvas.top="10" text="{binding x, stringformat='{}x = {0}'}" ishittestvisible="false" visibility="visible" x:name="xtext"/> <textblock canvas.left="30" canvas.top="25" text="{binding xmeaning}" ishittestvisible="false" visibility="visible" x:name="timetext"/> <textblock canvas.left="30" canvas.top="40" text="{binding y, stringformat='{}y = {0}'}" ishittestvisible="false" visibility="visible" x:name="ytext"/> </canvas> <controltemplate.triggers> <datatrigger binding="{binding isselected, relativesource={relativesource findancestor, ancestortype={x:type listboxitem}}}" value="true"> <setter targetname="rectangle" property="fill" value="red"/> </datatrigger> <trigger property="isdragging" value="true"> <setter targetname="rectangle" property="fill" value="green"/> </trigger> <datatrigger binding="{binding datacontext.showallcoordinates, relativesource={relativesource findancestor, ancestortype=window}}" value="true"> <setter targetname="xtext" property="visibility" value="visible"/> <setter targetname="ytext" property="visibility" value="visible"/> </datatrigger> <datatrigger binding="{binding }" value="true"> <setter targetname="rectangle" property="strokethickness" value="2"/> <setter targetname="rectangle" property="stroke" value="red"/> <setter targetname="rectangle" property="fill" value="white"/> </datatrigger> <multidatatrigger> <multidatatrigger.conditions> <condition binding="{binding isselected, relativesource={relativesource findancestor, ancestortype={x:type listboxitem}}}" value="true"/> <condition binding="{binding datacontext.showcurrentcoordinates, relativesource={relativesource findancestor, ancestortype=window}}" value="true"/> </multidatatrigger.conditions> <setter targetname="xtext" property="visibility" value="visible"/> <setter targetname="ytext" property="visibility" value="visible"/> </multidatatrigger> </controltemplate.triggers> </controltemplate> </thumb.template> </thumb> </canvas> </datatemplate> <!--connectors--> <datatemplate datatype="{x:type local:diagramconnector}"> <line stroke="orangered" strokethickness="3" x1="{binding start.x}" y1="{binding start.y}" x2="{binding end.x}" y2="{binding end.y}" x:name="line"/> <datatemplate.triggers> <datatrigger binding="{binding start}" value="{x:null}"> <setter targetname="line" property="visibility" value="collapsed"/> </datatrigger> </datatemplate.triggers> </datatemplate> </grid.resources> <grid> <grid.columndefinitions> <columndefinition width="auto"/> <columndefinition name="column1" width="*"/> <columndefinition width="auto"/> </grid.columndefinitions> <grid.rowdefinitions> <rowdefinition height="auto" /> <rowdefinition name="row1" height="*"/> <rowdefinition height="auto" /> <rowdefinition height="auto" /> <rowdefinition height="auto" /> </grid.rowdefinitions> <textblock margin="2" x:name="tbtitle" grid.column="1" grid.row="0" rendertransformorigin="0.5,0.5" fontsize="14" fontweight="bold" horizontalalignment="stretch" verticalalignment="stretch" textalignment="center" text="{binding graphaddress}"/> <textblock margin="2" x:name="tbxlabel" grid.column="1" grid.row="3" rendertransformorigin="0.5,0.5" textalignment="center" text="{binding xlabellabel}" /> <textblock margin="2" x:name="tbylabelleft" grid.column="0" grid.row="1" rendertransformorigin="0.5,0.5" textalignment="center" text="{binding ylabelleftlabel}"> <textblock.layouttransform> <rotatetransform angle="-90"/> </textblock.layouttransform> </textblock> <textblock margin="2" x:name="tbylabelright" grid.column="2" grid.row="1" rendertransformorigin="0.5,0.5" textalignment="center" text="{binding ylabelrightlabel}"> <textblock.layouttransform> <rotatetransform angle="-90"/> </textblock.layouttransform> </textblock> <listbox selecteditem="{binding selectedobject}" previewmousemove="listbox_previewmousemove" previewmousedown="listbox_previewmousedown" previewmouserightbuttondown="listbox_previewmouserightbuttondown" previewmouseleftbuttondown="listbox_previewleftmousedown" margin="10,10,10,10" grid.column="1" grid.row="1" x:name="lstbox"> <listbox.template> <controltemplate> <border> <border.background> <lineargradientbrush startpoint="0,0" endpoint="1,1" opacity=".3"> <gradientstop color="black" offset="0"/> <gradientstop color="lightgray" offset="1"/> </lineargradientbrush> </border.background> <scrollviewer verticalscrollbarvisibility="auto" horizontalscrollbarvisibility="auto"> <border> <border.background> <visualbrush tilemode="tile" viewport="0,0,36,36" viewportunits="absolute" viewbox="0,0,20,20" viewboxunits="absolute"> <visualbrush.visual> <rectangle stroke="darkgray" strokethickness="0.5" height="24" width="24" strokedasharray="5 3"/> </visualbrush.visual> </visualbrush> </border.background> <itemspresenter/> </border> </scrollviewer> </border> </controltemplate> </listbox.template> <listbox.itemssource> <staticresource resourcekey="diagramcol"/> </listbox.itemssource> <listbox.itemspanel> <itemspaneltemplate> <canvas isitemshost="true" background="#01ffffff" height="{binding areaheight}" width="{binding areawidth}" verticalalignment="bottom" horizontalalignment="left"/> </itemspaneltemplate> </listbox.itemspanel> <listbox.itemcontainerstyle> <style targettype="listboxitem"> <setter property="canvas.left" value="{binding x}"/> <setter property="canvas.top" value="{binding y}"/> <setter property="focusvisualstyle" value="{staticresource emptyfocusvisualstyle}"/> <setter property="template"> <setter.value> <controltemplate targettype="listboxitem"> <contentpresenter x:name="content"/> <controltemplate.triggers> <trigger property="isselected" value="true"> <setter targetname="content" property="effect"> <setter.value> <dropshadoweffect color="gray" shadowdepth="4" blurradius="10"/> </setter.value> </setter> </trigger> <datatrigger binding="{binding isnew}" value="true"> <setter property="opacity" value=".5"/> </datatrigger> </controltemplate.triggers> </controltemplate> </setter.value> </setter> </style> </listbox.itemcontainerstyle> </listbox> <listbox selecteditem="{binding selectedobject}" previewmousemove="listbox_previewmousemove" previewmousedown="listbox_previewmousedown" previewmouserightbuttondown="listbox_previewmouserightbuttondown" previewmouseleftbuttondown="listbox_previewleftmousedown" margin="10,10,10,10" grid.column="1" grid.row="4" x:name="sldbox"> <listbox.template> <controltemplate> <border> <border.background> <lineargradientbrush startpoint="0,0" endpoint="1,1" opacity=".3"> <gradientstop color="transparent" offset="0"/> </lineargradientbrush> </border.background> <scrollviewer verticalscrollbarvisibility="auto" horizontalscrollbarvisibility="auto"> <border> <border.background> <visualbrush tilemode="tile" viewport="0,0,0,11" viewportunits="absolute" viewbox="0,0,20,20" viewboxunits="absolute"> <visualbrush.visual> <rectangle stroke="darkgray" strokethickness="0.5" height="24" width="24" strokedasharray="5 3"/> </visualbrush.visual> </visualbrush> </border.background> <itemspresenter/> </border> </scrollviewer> </border> </controltemplate> </listbox.template> <listbox.itemssource> <staticresource resourcekey="slidercol"/> </listbox.itemssource> <listbox.itemspanel> <itemspaneltemplate> <canvas isitemshost="true" background="#01ffffff" height="50" width="{binding areawidth}" verticalalignment="bottom" horizontalalignment="left"/> </itemspaneltemplate> </listbox.itemspanel> <listbox.itemcontainerstyle> <style targettype="listboxitem"> <setter property="canvas.left" value="{binding x}"/> <setter property="canvas.top" value="10"/> <setter property="focusvisualstyle" value="{staticresource emptyfocusvisualstyle}"/> <setter property="template"> <setter.value> <controltemplate targettype="listboxitem"> <contentpresenter x:name="content"/> <controltemplate.triggers> <trigger property="isselected" value="true"> <setter targetname="content" property="effect"> <setter.value> <dropshadoweffect color="gray" shadowdepth="4" blurradius="10"/> </setter.value> </setter> </trigger> <datatrigger binding="{binding isnew}" value="true"> <setter property="opacity" value=".5"/> </datatrigger> </controltemplate.triggers> </controltemplate> </setter.value> </setter> </style> </listbox.itemcontainerstyle> </listbox> </grid> </grid>
please help. if question or misunderstanding, please ask:)
you can try
"{binding elementname=lstbox, path=itemssource.count, converter={staticresource itemssourcecountconverter}}"
and then, change elementname other listbox named sldbox. i've provided path, it's possible need property. follow example, add isselected one.
of course can rid of converter, i've added example.
Comments
Post a Comment