WPF ComboBox Dropdown Binding Implementation of the TreeView feature

Source: Internet
Author: User

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> &LT;/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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.