WPF 組織機構下拉樹多選

来源:https://www.cnblogs.com/s0611163/archive/2018/04/09/8758980.html
-Advertisement-
Play Games

XAML代碼: <UserControl x:Class="SunCreate.CombatPlatform.Client.MultiSelOrgTree" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns ...


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>
View Code

後臺代碼:

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;
    }
}
View Code

效果圖:

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...