c# - WPF- Combobox Binding -


i have class named "employee":

public string forname { get; set; } public string lastname { get; set; } public employeegroup group { get; set; } 

a class "employeegroup":

public string groupname { get; set; } public short groupid { get; set; } 

and wpf:

<combobox x:name="cmbgroup" selecteditem="{binding group}" horizontalalignment="left" margin="342,226,0,0" verticalalignment="top" width="129"/> <textbox x:name="txtforename" text="{binding forname}" horizontalalignment="left" height="24" margin="342,21,0,0" textwrapping="wrap" verticalalignment="top" width="129" verticalcontentalignment="center" gotfocus="selecttext"/> <textbox x:name="txtlastname" text="{binding lastname}" horizontalalignment="left" height="24" margin="342,47,0,0" textwrapping="wrap" verticalalignment="top" width="129" verticalcontentalignment="center" gotfocus="selecttext"/> <listbox x:name="lstemployee" horizontalalignment="left" height="362" margin="25,19,0,0" verticalalignment="top" width="217" selectionchanged="lstemployee_selectionchanged"/> 

the combobox , listbox gets source code-behind-file (a observablecollection):

cmbgroup.itemssource = database_contract.getlistofcontract(); lstemployee.itemssource = database_employee.getlistofemployee(); 

and set datacontext when employee selected in listbox in code-behind-file of wpf:

datacontext = lstemployee.selecteditem; 

the binding listbox , textboxes works fine, have problem combobox: source works, means can select different groups. there no binding selected employee. when select employee, textboxes fore- , lastname filled right, combobox ist emtpy. when click combobox, can choose given groups.

so i've done wrong?

edit: when set binding of combobox.selectedindex group.groupid, works. it's not guaranteed groupid , combobox-index same.

i think problem when set datacontext of whole view selected employee.
changing binding context of combo box , binding's changing.

i suggest give selectedemployee property in code , set in selecteditemchanged event , can change bindings text boxes read text="{binding selectedemployee.forname}"

you find values won't automatically update on screen when this, , because need implement inotifypropertychanged in form code, , call onpropertychanged property setters tell bindings refresh.


Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -