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];

        }

實現的效果基本能滿足項目需要了。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM