WPF 组织机构下拉树多选

XAML代码:

<UserControl x:Class="SunCreate.CombatPlatform.Client.MultiSelOrgTree"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:ui="clr-namespace:SunCreate.CombatPlatform.Client"
             mc:Ignorable="d"
             d:DesignHeight="25" d:DesignWidth="200">
    <Grid>
        <Button x:Name="btnSelected" Click="BtnClick" Height="25">
            <Button.Template>
                <ControlTemplate>
                    <Border Height="{TemplateBinding Property=Height}">
                        <Border.Background>
                            <ImageBrush ImageSource="/SunCreate.CombatPlatform.Client.Resources;component/Image/Face/Enter.png"/>
                        </Border.Background>
                        <TextBlock Margin="3 0 0 0" Text="{TemplateBinding Property=Tag}" Foreground="#1ba4f6" VerticalAlignment="Center"></TextBlock>
                    </Border>
                </ControlTemplate>
            </Button.Template>
        </Button>
        <Popup x:Name="popup" StaysOpen="False" PopupAnimation="Scroll" Width="280" Height="300" AllowsTransparency="True">
            <Border Background="#00234E" BorderThickness="1" BorderBrush="#224066">
                <TreeView x:Name="orgTree" >
                    <TreeView.Template>
                        <ControlTemplate>
                            <ScrollViewer HorizontalScrollBarVisibility="Auto" MinHeight="{Binding ElementName=orgTree,Path=ActualHeight}" MinWidth="{Binding ElementName=orgTree, Path=ActualWidth}">
                                <ItemsPresenter></ItemsPresenter>
                            </ScrollViewer>
                        </ControlTemplate>
                    </TreeView.Template>
                    <TreeView.ItemTemplate>
                        <HierarchicalDataTemplate DataType="{x:Type ui:MultiSelOrgTreeItemModel}" ItemsSource="{Binding Path=Nodes}"  >
                            <Border Height="25" Width="200">
                                <Grid VerticalAlignment="Center" HorizontalAlignment="Left">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="auto"></ColumnDefinition>
                                        <ColumnDefinition Width="15"></ColumnDefinition>
                                        <ColumnDefinition ></ColumnDefinition>
                                    </Grid.ColumnDefinitions>
                                    <CheckBox Visibility="{Binding Path=CheckVisiable}" Tag="{Binding}" IsChecked="{Binding Path=IsChecked, Mode=TwoWay}" Click="cxb_Node_Click" Checked="cxb_Node_Checked" Unchecked="cxb_Node_UnChecked"></CheckBox>
                                    <Image Grid.Column="1" Width="10" Height="10" Source="/SunCreate.CombatPlatform.Client.Resources;component/Image/orgIcon.png"></Image>
                                    <TextBlock Grid.Column="2" FontSize="12" VerticalAlignment="Center" Margin="10,0,0,0" x:Name="Name" Foreground="White" Text="{Binding Path=Name}"></TextBlock>
                                </Grid>
                            </Border>
                        </HierarchicalDataTemplate>
                    </TreeView.ItemTemplate>
                </TreeView>
            </Border>
        </Popup>
    </Grid>
</UserControl>

后台代码:

using SunCreate.pahf.Domain;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SunCreate.CombatPlatform.Client
{
    /// <summary>
    /// 组织机构树多选
    /// </summary>
    public partial class MultiSelOrgTree : UserControl
    {
        private bool _firstLoad = true;
        private log4net.ILog _log = log4net.LogManager.GetLogger(typeof(MultiSelOrgTree));
        private ObservableCollection<MultiSelOrgTreeItemModel> _collection = new ObservableCollection<MultiSelOrgTreeItemModel>();
        private ObservableCollection<PT_ORG_INFO> _selectedOrgs = new ObservableCollection<PT_ORG_INFO>();
        private bool _inited = false;

        public IList<PT_ORG_INFO> SelectedOrgs
        {
            get { return _selectedOrgs; }
        }

        public MultiSelOrgTree()
        {
            InitializeComponent();

            this.Loaded += MultiSelOrgTree_Loaded;
            this.orgTree.Loaded += orgTree_Loaded;
        }

        private void MultiSelOrgTree_Loaded(object sender, RoutedEventArgs e)
        {
            if (_firstLoad)
            {
                _firstLoad = false;
                InitData();
            }
        }

        private void InitData()
        {
            System.Threading.Tasks.Task.Factory.StartNew(() =>
            {
                var list = SP.Get<Cache.ICacheService>().OrgCache.GetCameraOrgs();
                var info = list.FirstOrDefault(p => p.PAR_ID == 0);
                MultiSelOrgTreeItemModel root = new MultiSelOrgTreeItemModel();
                root.Info = info;
                root.Name = info.ORG_NAME;
                BuildTree(root, list);
                _collection.Add(root);
                Dispatcher.BeginInvoke(new Action(() =>
                {
                    this.orgTree.ItemsSource = root.Nodes;
                    _inited = true;
                }));
            });
        }

        private void orgTree_Loaded(object sender, RoutedEventArgs e)
        {
            ExpandInternal(this.orgTree);
        }

        private void BuildTree(MultiSelOrgTreeItemModel root, IList<PT_ORG_INFO> orgs)
        {
            var children = orgs.Where(p => p.PAR_ID == root.Info.ID);
            if (children != null && children.Count() > 0)
            {
                foreach (var item in children)
                {
                    MultiSelOrgTreeItemModel model = new MultiSelOrgTreeItemModel();
                    model.Info = item;
                    model.Name = item.ORG_NAME;
                    model.IsChecked = false;
                    model.Parent = root;
                    BuildTree(model, orgs);
                    root.Nodes.Add(model);
                }
            }
            else
            {
                root.IsLeaf = true;
            }
        }

        /// <summary>
        /// 展开树节点
        /// </summary>
        /// <param name="targetItemContainer"></param>
        private void ExpandInternal(System.Windows.Controls.ItemsControl targetItemContainer)
        {
            try
            {
                if (targetItemContainer == null) return;
                if (targetItemContainer.Items == null) return;
                foreach (Object item in targetItemContainer.Items)
                {
                    System.Windows.Controls.TreeViewItem treeItem = targetItemContainer.ItemContainerGenerator.ContainerFromItem(item) as TreeViewItem;
                    var info = item as TreeNode;
                    if (treeItem == null || !treeItem.HasItems)
                    {
                        continue;
                    }
                    //if (info.Info == null)
                    //{
                    //    treeItem.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#2ad7fa"));
                    //}
                    treeItem.IsExpanded = true;
                    //ExpandInternal(treeItem as ItemsControl);
                }
            }
            catch (Exception ex)
            {
                _log.ErrorFormat("ExpandInternal error", ex);
            }
        }

        private void cxb_Node_UnChecked(object sender, RoutedEventArgs e) { }

        private void cxb_Node_Checked(object sender, RoutedEventArgs e) { }

        private void cxb_Node_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                CheckBox cbx = sender as CheckBox;
                var node = (sender as CheckBox).Tag as MultiSelOrgTreeItemModel;
                if (node != null && cbx.IsChecked != null)
                {
                    if (cbx.IsChecked.Value)
                    {
                        if (!_selectedOrgs.Contains(node.Info))
                        {
                            _selectedOrgs.Add(node.Info);
                            CheckChild(node, true);
                        }
                    }
                    else
                    {
                        if (_selectedOrgs.Contains(node.Info))
                        {
                            _selectedOrgs.Remove(node.Info);
                            CheckChild(node, false);
                        }
                    }
                    CheckParent(node);
                    btnSelected.Tag = string.Join(",", _selectedOrgs.ToList().ConvertAll(a => a.ORG_NAME));
                }
            }
            catch (Exception ex)
            {

            }
        }

        private void CheckParent(MultiSelOrgTreeItemModel node)
        {
            if (node.Parent != null)
            {
                bool isCheck = true;
                foreach (MultiSelOrgTreeItemModel item in node.Parent.Nodes)
                {
                    if (!item.IsChecked)
                    {
                        isCheck = false;
                    }
                }
                if (isCheck)
                {
                    node.Parent.IsChecked = true;
                    if (!_selectedOrgs.Contains(node.Parent.Info))
                    {
                        _selectedOrgs.Insert(0, node.Parent.Info);
                    }
                }
                else
                {
                    node.Parent.IsChecked = false;
                    if (_selectedOrgs.Contains(node.Parent.Info))
                    {
                        _selectedOrgs.Remove(node.Parent.Info);
                    }
                }
                btnSelected.Tag = string.Join(",", _selectedOrgs.ToList().ConvertAll(a => a.ORG_NAME));
                if (node.Parent.Parent != null)
                {
                    CheckParent(node.Parent);
                }
            }
        }

        private void CheckChild(MultiSelOrgTreeItemModel node, bool isCheck)
        {
            if (node.Nodes.Count > 0)
            {
                if (isCheck)
                {
                    foreach (MultiSelOrgTreeItemModel item in node.Nodes)
                    {
                        item.IsChecked = true;
                        if (!_selectedOrgs.Contains(item.Info))
                        {
                            _selectedOrgs.Add(item.Info);
                        }
                        CheckChild(item, true);
                    }
                }
                else
                {
                    foreach (MultiSelOrgTreeItemModel item in node.Nodes)
                    {
                        item.IsChecked = false;
                        if (_selectedOrgs.Contains(item.Info))
                        {
                            _selectedOrgs.Remove(item.Info);
                        }
                        CheckChild(item, false);
                    }
                }
            }
        }

        private void BtnClick(object sender, RoutedEventArgs e)
        {
            popup.PlacementTarget = sender as Button;
            popup.Placement = PlacementMode.Bottom;
            popup.IsOpen = true;
        }

        public void Select(List<string> orgIdList, ObservableCollection<MultiSelOrgTreeItemModel> Nodes = null)
        {
            System.Threading.Tasks.Task.Factory.StartNew(() =>
            {
                while (!_inited)
                {
                    System.Threading.Thread.Sleep(100);
                }
                this.Dispatcher.BeginInvoke(new Action(() =>
                {
                    if (Nodes == null)
                    {
                        foreach (MultiSelOrgTreeItemModel item in _collection[0].Nodes)
                        {
                            if (orgIdList.Exists(a => a == item.Info.ID.ToString()))
                            {
                                item.IsChecked = true;
                                if (!_selectedOrgs.Contains(item.Info))
                                {
                                    _selectedOrgs.Add(item.Info);
                                }
                            }
                            else
                            {
                                item.IsChecked = false;
                                if (_selectedOrgs.Contains(item.Info))
                                {
                                    _selectedOrgs.Remove(item.Info);
                                }
                            }
                            Select(orgIdList, item.Nodes);
                        }
                    }
                    else
                    {
                        foreach (MultiSelOrgTreeItemModel item in Nodes)
                        {
                            if (orgIdList.Exists(a => a == item.Info.ID.ToString()))
                            {
                                item.IsChecked = true;
                                if (!_selectedOrgs.Contains(item.Info))
                                {
                                    _selectedOrgs.Add(item.Info);
                                }
                            }
                            else
                            {
                                item.IsChecked = false;
                                if (_selectedOrgs.Contains(item.Info))
                                {
                                    _selectedOrgs.Remove(item.Info);
                                }
                            }
                            Select(orgIdList, item.Nodes);
                        }
                    }
                    btnSelected.Tag = string.Join(",", _selectedOrgs.ToList().ConvertAll(a => a.ORG_NAME));
                }));
            });
        }
    }

    public class MultiSelOrgTreeItemModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public MultiSelOrgTreeItemModel()
        {
            _nodes = new ObservableCollection<MultiSelOrgTreeItemModel>();
            _parent = null;
        }

        private ObservableCollection<MultiSelOrgTreeItemModel> _nodes;
        public ObservableCollection<MultiSelOrgTreeItemModel> Nodes
        {
            get { return _nodes; }
            set
            {
                this._nodes = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("Nodes"));
                }
            }
        }

        private MultiSelOrgTreeItemModel _parent;
        public MultiSelOrgTreeItemModel Parent
        {
            get { return _parent; }
            set
            {
                this._parent = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("Parent"));
                }
            }
        }

        /// <summary>
        /// 名称
        /// </summary>
        public string _name;
        public string Name
        {
            get { return _name; }
            set
            {
                this._name = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("Name"));
                }
            }
        }

        /// <summary>
        /// 是否是叶子
        /// </summary>
        public bool _isLeaf;
        public bool IsLeaf
        {
            get { return _isLeaf; }
            set
            {
                this._isLeaf = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("IsLeaf"));
                }
                if (value)
                {
                    CheckVisiable = Visibility.Visible;
                }
            }
        }

        /// <summary>
        /// 选择框是否可见
        /// </summary>
        public Visibility _checkVisiable = Visibility.Visible;
        public Visibility CheckVisiable
        {
            get { return _checkVisiable; }
            set
            {
                this._checkVisiable = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("CheckVisiable"));
                }
            }
        }

        /// <summary>
        /// 是否选中
        /// </summary>
        public bool _isChecked;
        public bool IsChecked
        {
            get { return _isChecked; }
            set
            {
                this._isChecked = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("IsChecked"));
                }
            }
        }

        public PT_ORG_INFO Info;
    }
}

效果图:

原文地址:https://www.cnblogs.com/s0611163/p/8758980.html

时间: 2024-10-08 14:59:39

WPF 组织机构下拉树多选的相关文章

WPF 组织机构下拉树多选,递归绑定方式现实

原文:WPF 组织机构下拉树多选,递归绑定方式现实 使用HierarchicalDataTemplate递归绑定现实 XAML代码: <UserControl x:Class="SunCreate.CombatPlatform.Client.MultiSelOrgTree" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas

Extjs下拉树代码测试总结

http://blog.csdn.net/kunoy/article/details/8067801 首先主要代码源自网络,对那些无私的奉献者表示感谢! 笔者对这些代码做了二次修改,并总结如下: Extjs3.x版本下拉树代码: [javascript] view plaincopy Ext.ux.TreeCombo = Ext.extend(Ext.form.ComboBox, { constructor : function(cfg) { cfg = cfg || {}; Ext.ux.Tr

jquery实现下拉框多选

一.说明 本文是利用EasyUI实现下拉框多选功能,在ComboxTree其原有的基础上对样式进行了改进,样式表已上传demo,代码如下 二.代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w

ops-web运维平台-create.jsp-mootools下拉框-复选框

create.jsp页面的,body部分 <body onload="Page.init('${pageError}','${pageMessage}',${isSubmit},true)"> <div id="title">${pageTitle}</div> <s:form id="MYFORM" action="%{pageAction}" method="post&q

EXTJS下拉树ComboBoxTree参数提交及回显方法

http://blog.csdn.net/wjlht/article/details/6085245 使用extjs可以构造出下拉数,但是不方便向form提交参数,在此,笔者想到一个办法,很方便ComboBoxTree向form提交. 原理: 在form中增加一个隐藏的字段,当在comboBoxTree中选定值后自动在隐藏字段中赋值. 为实现此方法,需要重载comboBoxTree中collapse事件方法. Ext.ux.ComboBoxTree = function(){    this.t

bootstrap-select实现下拉框多选效果

bootstrap-select实现下拉框多选效果 听语音 在学习bootstrap实现下拉多选效果的时候,觉得该效果很好,所以拿来分享下,这里就不详细的描述了,直接附上代码给各位看看 方法/步骤 1 最终实现的效果: 2 HTML代码: <div class= "row" style ="margin-top :10px;"> <div class= "form-group col-xs-12"> <label f

c# - Winform DatagridView上显示下拉树

Winform的DatagridView是不支持下拉树的,所以需要扩展 废话不多说,直接贴代码 首先需要对comBox扩展,下拉内容变成TreeView using System.Drawing;using System.Windows.Forms;namespace WindowsApplication23{ public class ComboBoxTreeView : ComboBox { private const int WM_LBUTTONDOWN = 0x201, WM_LBUTT

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

因为项目需要,接触到这个功能点,借助网络还有自己的一点摸索,实现了这个功能.相关代码如下: XAML部分的代码: <ComboBox Grid.Row="0" Grid.Column="9" HorizontalAlignment="Left" Name="OrgaComboBox" Margin="6" VerticalAlignment="Top" Width="20

Asp.net下拉树实现(Easy UI ComboTree)

场景描述:某个公司有多个部门并且部门存在子部门,通过一个下拉框选取多个部门,但是如果某个部门的子部门被全部选择,则只取该部门,而忽略子部门.(叶子节点全被选中时,只取父节点) 知识点:ComboTree.一般处理程序.递归.Json 效果如图 数据库表设计:unit_main unit_id unit_name parent_id desc 部门ID 部门名称 父ID 说明 节点类设计: 1 public class Unit 2 { 3 public decimal id { get; set