WPF ComboBox下拉绑定Treeview 功能的实现

2021-05-12 10:27

阅读:545

标签:container   hiera   cti   tostring   lte   cal   block   for   select   

因为项目需要,接触到这个功能点,借助网络还有自己的一点摸索,实现了这个功能。相关代码如下:

 

XAML部分的代码:

"0"  Grid.Column="9" HorizontalAlignment="Left" Name="OrgaComboBox" Margin="6"   VerticalAlignment="Top" Width="200" RenderTransformOrigin="0.392,0.565" DropDownClosed="OrgaComboBox_DropDownClosed">               
                "Collapsed">"lftTree" Margin="0" ItemsSource="{Binding}"  SelectedItemChanged="lftTree_SelectedItemChanged"                                                                                     
                                          DisplayMemberPath="OrgName" SelectedValuePath="OrgId" >
                                "{Binding Children}">
                                        "{Binding OrgName}">

后台相关代码:

ObservableCollection orgaCollection = new ObservableCollection();

List iorganizations = this.serviceAgent.QueryRootOrganizations();
            //List iorganizations = this.localDataAccess.QueryRootOrganizations();
            if (iorganizations == null)
            {
                return;
            }
            foreach (IOrganization current in iorganizations)
            {
                OrgaViewModel orgaVM = new OrgaViewModel
                {
                    IsExpanded = true,
                    OrgCode = current.OrgCode,
                    OrgId = current.OrgId,
                    OrgName = current.OrgName,
                    ParentOrgId = current.ParentOrgId
                };
                GetChildOrganization(orgaVM);
                orgaCollection.Add(orgaVM);
            }
            this.OrgaComboBox.DataContext = orgaCollection;

为了选中树的某个节点,能在ComboBox中显示数据,分别用了树和下拉框的一个控件事件:

private void lftTree_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgsobject> e)
        {

            try
            {
                
                tempOVM = (OrgaViewModel)e.NewValue;
                selectedOrgName = tempOVM.OrgName;
                selectedOrgId = tempOVM.OrgId;               

            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
            }

        }

 private void OrgaComboBox_DropDownClosed(object sender, EventArgs e)
        {
            OrgaComboBox.Items[0] = selectedOrgName;
            OrgaComboBox.SelectedItem = OrgaComboBox.Items[0];

        }

实现的效果基本能满足项目需要了。

 

WPF ComboBox下拉绑定Treeview 功能的实现

标签:container   hiera   cti   tostring   lte   cal   block   for   select   

原文地址:http://www.cnblogs.com/tlduck/p/7580697.html


评论


亲,登录后才可以留言!