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


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

    XAML部分的代码:

    <ComboBox Grid.Row="0"  Grid.Column="9" HorizontalAlignment="Left" Name="OrgaComboBox" Margin="6"   VerticalAlignment="Top" Width="200" 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 TargetType="TreeViewItem">
                                            <Setter Property="IsExpanded" Value="{Binding IsExpand}"></Setter>
                                        </Style>
                                    </TreeView.ItemContainerStyle>
                                    <TreeView.ItemTemplate>
                                        <HierarchicalDataTemplate  ItemsSource="{Binding Children}">
                                            <TextBlock  Text="{Binding OrgName}"></TextBlock>
                                        </HierarchicalDataTemplate>
                                    </TreeView.ItemTemplate>
                                </TreeView>
                            </ControlTemplate>
                        </ComboBoxItem.Template>
                    </ComboBoxItem>
                </ComboBox>

    后台相关代码:

    ObservableCollection<OrgaViewModel> orgaCollection = new ObservableCollection<OrgaViewModel>();
    
    List<IOrganization> iorganizations = this.serviceAgent.QueryRootOrganizations();
                //List<Organization> 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, RoutedPropertyChangedEventArgs<object> 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];
    
            }

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

  • 相关阅读:
    Java 工程转 C#
    初涉Linux ----------> 打造自己的 Vim IDE
    初涉Linux ----------> Ubuntu15.04的安装与美化
    没学过CSS等前端的我,也想美化一下自己的博客
    作为程序员之 Vim(一)
    win7升win10,初体验
    作为程序员之正则表达式
    数据库系统原理
    Mysql数据库笔记
    我的个人常用快捷键
  • 原文地址:https://www.cnblogs.com/tlduck/p/7580697.html
Copyright © 2020-2023  润新知