Because of the project needs, access to this function point, with the help of the network and their own little groping, the realization of this function. The relevant code is as follows:
Code for the XAML section:
<combobox grid.row="0"grid.column="9"Horizontalalignment=" Left"Name="Orgacombobox"margin="6"Verticalalignment="Top"Width=" $"rendertransformorigin="0.392,0.565"Dropdownclosed="orgacombobox_dropdownclosed"> <comboboxitem visibility="collapsed"></ComboBoxItem> <ComboBoxItem> <ComboBoxItem.Template> <ControlTemplate> <treeview name="Lfttree"margin="0"Itemssource="{Binding}"Selecteditemchanged="lfttree_selecteditemchanged" DisplayMemberPath="OrgName"Selectedvaluepath="OrgId"> <TreeView.ItemContainerStyle> <style targe Ttype="TreeViewItem"> <setter property="isexpanded"Value="{Binding Isexpand}"></Setter> </Style> </treeview.itemcon Tainerstyle> <TreeView.ItemTemplate> "{Binding Children}"> <textblock text="{Binding OrgName}"></TextBlock> </HierarchicalDataTemplate> </TreeView.ItemTemplate> </TreeView> </CONTROLTEMPLATE&G T </ComboBoxItem.Template> </ComboBoxItem> </ComboBox>
Background related code:
Observablecollection<orgaviewmodel> orgacollection =NewObservablecollection<orgaviewmodel>(); List<IOrganization> iorganizations = This. Serviceagent.queryrootorganizations (); //list<organization> iorganizations = This.localDataAccess.QueryRootOrganizations (); if(Iorganizations = =NULL) { return; } foreach(Iorganization currentinchiorganizations) {Orgaviewmodel ORGAVM=NewOrgaviewmodel {isexpanded=true, Orgcode=Current.orgcode, OrgId=Current.orgid, OrgName=Current.orgname, Parentorgid=Current . Parentorgid}; Getchildorganization (ORGAVM); Orgacollection.add (ORGAVM); } This. Orgacombobox.datacontext = orgacollection;
To select a node in the tree, you can display the data in the ComboBox, using a control event for the tree and drop-down boxes, respectively:
Private voidLfttree_selecteditemchanged (Objectsender, routedpropertychangedeventargs<Object>e) {Try{TEMPOVM=(Orgaviewmodel) E.newvalue; Selectedorgname=Tempovm.orgname; Selectedorgid=Tempovm.orgid; } Catch(Exception ex) {logger. Error (ex. ToString ()); } } Private voidOrgacombobox_dropdownclosed (Objectsender, EventArgs e) {orgacombobox.items[0] =Selectedorgname; Orgacombobox.selecteditem= orgacombobox.items[0]; }
The implementation of the effect is basically to meet the needs of the project.
WPF ComboBox Dropdown Binding Implementation of the TreeView feature