wpf - Binding to 2 Datasources -
i want bind datatemplate 2 datasources, 1 datasource define in listbox , other determine how many listboxes there , items in listbox selected\checked.
i have following xaml
<window x:class="wpfapplication1.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" title="mainwindow" height="350" width="525"> <window.resources> <datatemplate x:key="tokenlisttemplate"> <stackpanel orientation="horizontal"> <checkbox x:name="chktoken" ischecked="{binding path=isselected, mode=twoway}"> <textblock text="{binding path=text}" /> </checkbox> </stackpanel> </datatemplate> <datatemplate x:key="itemtemplate"> <border borderthickness="1"> <stackpanel margin="3"> <textblock text="{binding path=header}"/> <listbox itemtemplate="{staticresource tokenlisttemplate}" itemssource="{binding path=tokens}" > </listbox> </stackpanel> </border> </datatemplate> </window.resources> <grid> <listbox itemtemplate="{staticresource itemtemplate}" itemssource="{binding}"> <listbox.itemspanel> <itemspaneltemplate> <wrappanel/> </itemspaneltemplate> </listbox.itemspanel> </listbox> </grid> , codebehind
public partial class mainwindow : window { public mainwindow() { initializecomponent(); observablecollection<dataentity> _actualobjects; list<token> tokens1 = new list<token>() { new token("1"), new token("2"), new token("3"), new token("4") }; list<token> tokens2 = new list<token>() { new token("11"), new token("21"), new token("31") }; _actualobjects = new observablecollection<dataentity>() { new dataentity(tokens1, "a", "1,2,3", 1), new dataentity(tokens1, "b", "2,3", 1), new dataentity(tokens2, "c", "21,31", 2) }; datacontext = _actualobjects; } class dataentity { public dataentity(list<token> tokens, string header, string tokenstring, int entitytypeid) { tokens = tokens; header = header; tokenstring = tokenstring; entitytypeid = entitytypeid; } public list<token> tokens { get; set; } public string header { get; set; } public string tokenstring { get; set; } public int entitytypeid { get; set; } } public class token { public bool isselected { get; set; } public string text { get; set; } public token(string text) { this.isselected = false; this.text = text; } } } it produces 
i don't want inject token1 or token2 list dataentity object in other words want dataentity constructor
public dataentity(string header, string tokenstring, int entitytypeid) listbox datatemplate should select
- tokens1 list datasource lisboxitems if dataentity.entitytypeid = 1
- tokens2 list datasource lisboxitemsif dataentity.entitytypeid = 2
also tokenstring in dataentity should bound items in listbox i.e. if listbox shows 1 2 3 4 , dataentity listbox has tokenstring value set "1,2,3" 1 2 3 should checked in listbox 
i recommend create viewmodel layer between model , view. in viewmodel can arrange data fit used controls without changing model.
so viewmodel example split tokenstring of dataentity list of tokens.
just google mvvm (model-view-viewmodel) examples , furter explanations or here on (like mvvm: tutorial start finish?).
Comments
Post a Comment