c# - ObservableCollection always gets only first item in it for each created TabItem -
can point on mistake pleas. when add tabs each tab duplicates data in textboxes added in first created tab. observablecollection gets first item in each tab.
i have mainview tabcontrol wich add tabs programmatically , set content them through button_click
<window x:class="test.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:y="clr-namespace:test" title="mainwindow" height="350" width="525"> <grid> <toolbar height="40" verticalalignment="top"> <button foreground="aliceblue" fontweight="normal" click="button_click" fontsize="14" fontfamily="fixed miriam transparent">menu <button.contextmenu> <contextmenu > <menuitem header="add invoice" click="addinvoice_click"/> <menuitem header="invoices list" click="invoiceslist_click"/> </contextmenu> </button.contextmenu> </button> <toolbar.background> <lineargradientbrush endpoint="1,0.5" startpoint="0,0.5"> <gradientstop color="black" offset="0" /> <gradientstop color="#ff173ade" offset="0.431" /> <gradientstop color="#ff0b1d6f" offset="0.646" /> </lineargradientbrush> </toolbar.background> </toolbar> <dockpanel margin="2,46,0,0" name="dockpanel1" width="auto"> <grid> <tabcontrol name="tabcon" > </tabcontrol> </grid > </dockpanel> </grid>
through button_click add tabs, setting content them , datacontext controls
public partial class mainwindow : window { public mainwindow() { initializecomponent(); } mvm mvm = new mvm(); mvm2 mvm2 = new mvm2(); private void button_click(object sender, routedeventargs e) { (sender button).contextmenu.isenabled = true; (sender button).contextmenu.placementtarget = (sender button); (sender button).contextmenu.placement = system.windows.controls.primitives.placementmode.bottom; (sender button).contextmenu.isopen = true; } private void addinvoice_click(object sender, routedeventargs e) { mvm2.invoice_items.add(new vm2()); mvm.itemz.add(new vm()); var tabitem = new tabitem(); tabview tv = new tabview(); tabitem.content = tv; string s = string.format("tab"); mvm.itemz.add(new vm(s)); xmx.itemz.add(new vm()); tv.datagrid1.datacontext = mvm2; tv.listbox1.datacontext = mvm; tabcon.items.add(tabitem); if (tabcon.selectedindex == null) { tabcon.selectedindex = -1; } tabcon.selectedindex++; } }
}
this view tabs
<usercontrol x:class="test.tabview" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:views="clr-namespace:test" mc:ignorable="d" d:designheight="600" d:designwidth="800"> <grid > <grid.rowdefinitions> <rowdefinition height="*" minheight="300" /> <rowdefinition height="*" minheight="300" /> </grid.rowdefinitions> <grid.columndefinitions> <columndefinition width="*" /> </grid.columndefinitions> <datagrid autogeneratecolumns="false" grid.columnspan="5" grid.row="3" height="104" horizontalalignment="stretch" margin="55,115,55,0" name="datagrid1" verticalalignment="top" width="auto" canuserresizerows="true" itemssource="{binding path = invoice_items}" isreadonly="false" selectionunit="cell" canuseraddrows="true" issynchronizedwithcurrentitem="true"> <datagrid.columns> <datagridtextcolumn header="item name" binding="{binding item_name, mode=twoway}"/> </datagrid.columns> </datagrid> <listbox itemssource="{binding itemz}" selectedindex="{binding selectedtabindex, mode=twoway}" scrollviewer.horizontalscrollbarvisibility="disabled" horizontalalignment="stretch" name="listbox1" verticalalignment="top" isenabled="true" focusable="true" selectionmode="single" verticalcontentalignment="stretch" minwidth="768" minheight="446" opacity="1" horizontalcontentalignment="stretch"> <listbox.borderbrush> <solidcolorbrush /> </listbox.borderbrush> <listbox.itemtemplate> <datatemplate> <views:datagrrr></views:datagrrr> </datatemplate> </listbox.itemtemplate> <listbox.background> <solidcolorbrush /> </listbox.background> <listbox.resources> <style targettype="{x:type listboxitem}"> <setter property="template"> <setter.value> <controltemplate targettype="{x:type listboxitem}"> <border x:name="bd" borderbrush="{templatebinding borderbrush}" borderthickness="{templatebinding borderthickness}" background="{templatebinding background}" padding="{templatebinding padding}" snapstodevicepixels="true"> <contentpresenter horizontalalignment="{templatebinding horizontalcontentalignment}" snapstodevicepixels="{templatebinding snapstodevicepixels}" verticalalignment="{templatebinding verticalcontentalignment}" /> </border> <controltemplate.triggers> <multitrigger> <multitrigger.conditions> <condition property="selector.isselectionactive" value="false" /> <condition property="isselected" value="true" /> </multitrigger.conditions> <setter property="background" targetname="bd" value="transparent" /> </multitrigger> <multitrigger> <multitrigger.conditions> <condition property="selector.isselectionactive" value="true" /> <condition property="isselected" value="true" /> </multitrigger.conditions> <setter property="background" targetname="bd" value="transparent" /> </multitrigger> </controltemplate.triggers> </controltemplate> </setter.value> </setter> </style> </listbox.resources> </listbox> </grid>
public partial class tabview : usercontrol { mvm xmx = new mvm(); mvm2 mvm = new mvm2(); public tabview() { datagrid1 = new datagrid(); initializecomponent(); //mvm.invoice_items.add(new vm2()); //xmx.itemz.add(new vm()); //listbox1.datacontext = xmx; //datagrid1.datacontext = mvm; } }
and view listbox
<usercontrol x:class="test.datagrrr" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:ignorable="d" d:designheight="456" d:designwidth="887"> <grid name="grid1" horizontalalignment="stretch" verticalalignment="stretch" minwidth="768" minheight="446" focusable="true" height="auto"> <grid.rowdefinitions> <rowdefinition minheight="43" height="*" /> <rowdefinition height="*" minheight="45" /> <rowdefinition height="*" minheight="45" /> <rowdefinition height="*" minheight="170" /> </grid.rowdefinitions> <grid.columndefinitions> <columndefinition width="*" /> <columndefinition width="*" /> <columndefinition width="*" minwidth="100" /> <columndefinition width="*" /> <columndefinition width="*" /> </grid.columndefinitions> <textbox text="{binding supplier, mode=twoway}" grid.column="1" height="27" name="textbox1" verticalalignment="top" margin="11,6,0,0" horizontalalignment="stretch" width="auto" fontsize="14" horizontalcontentalignment="stretch" minwidth="141" flowdirection="lefttoright" datacontext="{binding}" /> <label content="supplier" height="27" name="label2" verticalalignment="top" fontsize="14" fontfamily="tahoma" fontweight="bold" margin="21,6,0,0" width="auto" isenabled="true" horizontalalignment="stretch" foreground="black" background="white" minwidth="133" horizontalcontentalignment="stretch" /> </grid>
one of observablecollection
class mvm : bvm { private observablecollection<vm> items = new observablecollection<vm>(); public observablecollection<vm> itemz { { return items; } set { items = value; onpropertychanged("itemz"); } }
}
and model
class vm: bvm { private string supplier; public vm() { } public string supplier { { return supplier; } set { if (supplier != value) { supplier = value; onpropertychanged("supplier"); } } } }
use list , check result, think problem not in observablecollection, problem in class thet binding tab, works if bind 1 object every new tab, static
Comments
Post a Comment