c# - DataBinding of a ComboBox after InitializeComponent() -
i have 2 combobox
on code:
<combobox name="comboboxselectcamera" itemssource="{binding path=listcameras}" selectionchanged="comboboxselectcamera_selectionchanged" /> <combobox name="comboboxcities" itemssource="{binding path=listcities}" />
on window
have code combobox
understand path
comes from:
<window .... datacontext="{binding relativesource={relativesource self}}" .... >
both combos binded 2 listes created on mainwindow
:
public mainwindow() { initializecitiescombo(); initializecomponent(); // initialize control checks cameras initializecameracontrol(); fillcameraproperties(); datacontext = this; }
the first combobox
list created before initializecomponent
, when combo created, fills specific content.
the second combobox
list created after initializecomponent
, because depend on object loads cameras, and, don't know if that's reason, if list created after initializecomponent
, doesn't fill combobox
.
i know there other ways fill list of connected cameras on computer, i have use control created on initializecomponent
, initiated on initializecameracontrol()
.
any idea on how can fill combobox
after initializecomponent
?
edit: declaration of both lists:
private list<camerainfo> listcameras { get; set; } private list<string> listcities { get; set; }
somewhere in code, listcites
:
listcities = new list<string> { madrid, barcelona, alicante, valencia }
somewhere in code, control returns list of connected cameras (it works, has elements don't display on combo):
listcameras = myusercontrol.connectedcameras; // returns valid list of `camerainfo`
my camerainfo
class:
public class camerainfo { public camerainfo(string name, string id); public static implicit operator string(camerainfo camerainfo); public string id { get; } public string name { get; } public override string tostring(); }
note while discuss this, tried change on combo of cameras displaymemberpath
.
binding error:
system.windows.data error: 40 : bindingexpression path error: 'text' property not found on 'object' ''mainwindow' (name='')'. bindingexpression:path=text; dataitem='mainwindow' (name=''); target element 'textbox' (name=''); target property 'template' (type 'controltemplate')
ok, there major problem. first, change lists this.
private observablecollection<camerainfo> _listcamera; public observablecollection<camerainfo> listcameras { { return _listcamera ?? (_listcamera = new observablecollection<camerainfo>()); } }
i'm showing 1 list, have both. fill list, don't reinstantiate them, clear them. e.g.
private void fillcamerainfo() { ienumerable<camerainfo> example = new[] { new camerainfo(), new camerainfo() }; listcameras.clear(); foreach(var exampleentry in example) listcameras.add(exampleentry); }
it's example, kept simple possible. if reinstantiate them bindings 'destroyed'.
that first combobox
filled correctly lies in fact you're filling it, before initialize components bindings. items can found. second combobox
not aware of changes in listcities
, because it's not notified it. if use observablecollection<t>
combobox
notified, please have here more information. , binding should work expected.
edit - why shouldn't reinstantiate collections
if following put data collection
public fillcamerainfo() { listcameras = new observablecollection<camerainfo>(somedata); }
the bindings still listen 'old' instance. changed reference of listcameras
, nobody listens new instance.
edit - why don't see anything
ok, bit odd, it's is. order of declartion of combobox
binding pretty important, works properly. please have here right order.