树形的选中事件

作者:李坤凤
本次任务完成时间:2020年5月07日
开发工具与关键技术:开发工具:Visual Studio 关键技术:树形的选中事件

1、查询树形,通过获取Id查询出所需要的数据

 private List<Node> GetNodeList()
        {
            List<Node> TwoNodes = new List<Node>();//第二层节点列表
            List<Node> ThreeNodes = new List<Node>();//第三层节点列表
            int intWardID = 0;
            //二级节点:查询病区信息
            var wards = myModel.B_Ward.OrderBy(c => c.WardID).ToList();
            for (int i = 0; i < wards.Count(); i++)
            {
                //三级节点:查询病区住院病人
                intWardID = wards[i].WardID;
                var mypatiens = (from Behospitalized in myModel.B_Behospitalized
                                 join Patient in myModel.B_Patient on Behospitalized.PatientID equals Patient.PatientID
                                 join Bed in myModel.B_Bed on Behospitalized.BedID equals Bed.BedID
                                 join Sex in myModel.D_AttributeDetails on Patient.A_Sex equals Sex.AttributeDetailsID
                                 where Behospitalized.Effect == true && Bed.State == 2 //
                                 orderby Bed.BedID ascending//
                                 where Behospitalized.WardID == intWardID
                                 select new Behospitalizeds
                                 {
                                     BehospitalizedID = Behospitalized.BehospitalizedID,
                                     PatientID = Behospitalized.PatientID,
                                     BedID = Behospitalized.BedID,
                                     InpatientNumber = Behospitalized.InpatientNumber,
                                     PatientName = Patient.PatientName,
                                     Sex = Sex.DetailsName,
                                     Bed = Bed.BedName
                                 }).ToList();
                ThreeNodes = new List<Node>();//每次进来的都要实例化一下子节点
                for (int j = 0; j < mypatiens.Count; j++)
                {
                    //遍历生成病区节点
                    Node twoLevelNode = new Node();
                    twoLevelNode.NodeId = mypatiens[j].PatientID.ToString();
                    twoLevelNode.NodeName = mypatiens[j].PatientName + "(" + mypatiens[j].Sex + ")" + mypatiens[j].Bed;
                    twoLevelNode.NodeContent = mypatiens[j].InpatientNumber;
                    twoLevelNode.NodeType = NodeType.LeafNode;
                    twoLevelNode.Nodes = new List<Node>();
                    twoLevelNode.Icon = "/../../Images/Main/offline_user.ico";
                    ThreeNodes.Add(twoLevelNode);
                }
                //遍历生成病区节点
                Node firstLevelNode = new Node();
                firstLevelNode.NodeName = wards[i].WardName;
                firstLevelNode.NodeContent = wards[i].WardName;
                firstLevelNode.NodeType = NodeType.LeafNode;
                firstLevelNode.Nodes = ThreeNodes;//把三级节点病人信息添加到二级节点
                firstLevelNode.Icon = "/../../Images/HospitalWork/png-0522.png";
                TwoNodes.Add(firstLevelNode);
            }
            return new List<Node>()
            {
                //二级节点:住院病人一览表
                new Node()
                {
                    NodeName ="住院病人一览表",
                    NodeContent = " 住院病人一览表",
                    NodeType = NodeType.RootNode,
                    Nodes = TwoNodes,//
                    Icon ="/../../Images/HospitalWork/png-0522.png",
                }
            };
        }

2、绑定节点数据

private void ExpandTree()
        {
            if (this.TreeView_Patient.Items !=null && this.TreeView_Patient.Items.Count > 0)
            {
                foreach (var item in this.TreeView_Patient.Items)
                {
                    DependencyObject dependencyObject = this.TreeView_Patient.ItemContainerGenerator.ContainerFromItem(item);
                    if (dependencyObject !=null)//第一次打开程序dependencyObject为null,会出错
                    {
                        ((TreeViewItem)dependencyObject).ExpandSubtree();
                    }
                }
            }
        }

  private void SelectTheCurrentNode()
        {
            if (this.TreeView_Patient.Items !=null && this.TreeView_Patient.Items.Count > 0)
            {
                foreach (var item in this.TreeView_Patient.Items)
                {
                    DependencyObject dependencyObject = this.TreeView_Patient.ItemContainerGenerator.ContainerFromItem(item);
                    if (dependencyObject != null)
                    {
                        TreeViewItem tvi = (TreeViewItem)dependencyObject;
                        if ((tvi.Header as Node).NodeId== selectedNode.NodeId)
                        {
                            tvi.IsSelected = true;
                            tvi.Focus();
                        }
                        else
                        {
                            SetNodeSelected(tvi);
                        }
                    }
                }
            }
        }
        //SetNodeSelected
        private void SetNodeSelected(TreeViewItem Item)
        {
            foreach (var item in Item.Items)
            {
                DependencyObject dependencyObject = Item.ItemContainerGenerator.ContainerFromItem(item);
                if (dependencyObject != null)
                {
                    TreeViewItem treeViewItem = (TreeViewItem)dependencyObject;
                    if ((treeViewItem.Header as Node).NodeId == selectedNode.NodeId)
                    {
                        treeViewItem.IsSelected = true;
                        treeViewItem.Focus();
                    }
                    else
                    {
                        treeViewItem.IsSelected = false;
                        if (treeViewItem.HasItems)
                        {
                            SetNodeSelected(treeViewItem);
                        }
                    }
                }
            }
        }

3、树形节点选中项改变事件

   private void TreeView_Patient_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            //实例化当前选择的树形控件
            Node node = this.TreeView_Patient.SelectedItem as Node;
            //获取当前选中的病人信息
            selectedNode = node;
            //获取病人信息里面的入院ID
            try
            {
                btnBookKeepingBag.IsEnabled = true;
                btnBookKeepingBag.Opacity = 1;
                btnUpdate.IsEnabled = true;
                btnUpdate.Opacity = 1;
                btnUInpatientNumber.IsEnabled = true;
                btnUInpatientNumber.Opacity = 1;
                intPatientId = Convert.ToInt32(selectedNode.NodeId);
                //查询病人基本信息
                var dbPatient = (from tbPatient in myModel.B_Patient
                                 join tbBehospitalized in myModel.B_Behospitalized on tbPatient.PatientID equals tbBehospitalized.PatientID
                                 join tbInsurance in myModel.B_Insurance on tbPatient.PatientID equals tbInsurance.PatientID
                                 where tbPatient.PatientID == intPatientId && tbInsurance.Effect == true
                                 select new PatientEntitys
                                 {
                                     PatientID = tbPatient.PatientID,
                                     PatientName = tbPatient.PatientName,
                                     PatientIDCard = tbPatient.PatientIDCard,
                                     A_Sex = tbPatient.A_Sex,
                                     Age = tbPatient.Age,
                                     Birthday = tbPatient.Birthday,
                                     A_NationalityID = tbPatient.A_NationalityID,
                                     A_NationID = tbPatient.A_NationID,
                                     IdAddress = tbPatient.IdAddress,
                                     A_MaritalStatusID = tbPatient.A_MaritalStatusID,
                                     MobilePhone = tbPatient.MobilePhone,
                                     ContactAddress = tbPatient.ContactAddress,
                                     Contacts = tbPatient.Contacts,
                                     Occupation = tbPatient.Occupation,
                                     Guarantor = tbPatient.Guarantor,
                                     Unit = tbPatient.Unit,
                                     UnitCode = tbPatient.UnitCode,
                                     UnitPhone = tbPatient.UnitPhone,
                                     Relationship = tbPatient.Relationship,
                                     PhoneNumber = tbPatient.PhoneNumber,
                                     Postcode = tbPatient.Postcode,
                                     Spell = tbPatient.Spell,
                                     Wubi = tbPatient.Wubi,
                                     InsuranceID = tbInsurance.InsuranceID,
                                     CardNumber = tbInsurance.CardNumber,//医保卡号
                                     A_CarTypeID = tbInsurance.A_CarTypeID,
                                     A_ChargeTypeID = tbInsurance.A_ChargeTypeID,
                                     DistrictCode = tbInsurance.DistrictCode,
                                     InseranceUnit = tbInsurance.Unit,
                                     CertificateNumber = tbInsurance.CertificateNumber,
                                     MedicalProgramID = tbInsurance.MedicalProgramID,
                                     CertificatDiagnosis = tbInsurance.CertificatDiagnosis,
                                     StartDate = tbInsurance.StartDate,
                                     EndDate = tbInsurance.EndDate,
                                     JUnit = tbInsurance.Unit,
                                     InpatientNumber = tbBehospitalized.InpatientNumber,
                                     A_PatientTypeID = tbBehospitalized.A_PatientTypeID,
                                     WardID = tbBehospitalized.WardID,
                                 }).FirstOrDefault();
                //患者信息回填
                txtAge.Text = dbPatient.Age;
                txtCertificatDiagnosis.Text = dbPatient.CertificatDiagnosis;
                txtCertificateNumber.Text = dbPatient.CertificateNumber;
                txtContactAddress.Text = dbPatient.ContactAddress;
                txtContacts.Text = dbPatient.Contacts;
                txtDistrictCode.Text = dbPatient.DistrictCode;
                txtIdAddress.Text = dbPatient.IdAddress;
                txtIdCard.Text = dbPatient.PatientIDCard;
                txtInpatientNumber.Text = dbPatient.InpatientNumber;
                txtMedicalCard.Text = dbPatient.CardNumber;
                txtOccupation.Text = dbPatient.Occupation;
                txtPatientName.Text = dbPatient.PatientName;
                txtPhoneNumber.Text = dbPatient.PhoneNumber;
                txtUnit.Text = dbPatient.Unit;
                txtYouBian.Text = dbPatient.Postcode;
                txtSpell.Text = dbPatient.Spell;
                txtUnitPhone.Text = dbPatient.UnitPhone;
                txtWubi.Text = dbPatient.Wubi;
                txtSBCardNumber.Text = dbPatient.CardNumber;
                dtpStartDate.Text = dbPatient.StartDate.ToString();
                dtpEndDate.Text = dbPatient.EndDate.ToString();
                cboSex.SelectedValue = dbPatient.A_Sex;
                cboMaritalStatus.SelectedValue = dbPatient.A_MaritalStatusID;
                cboNationality.SelectedValue = dbPatient.A_NationalityID;
                cboCarType.SelectedValue = dbPatient.A_CarTypeID;
                cboChargeType.SelectedValue = dbPatient.A_ChargeTypeID;
                cboMedicalProgram.SelectedValue = dbPatient.MedicalProgramID;
                cboPatientType.SelectedValue = dbPatient.A_PatientTypeID;
                txtJUnit.Text = dbPatient.JUnit;//记账单位
                intWardId = Convert.ToInt32(dbPatient.WardID);//病区ID
                if (Convert.ToInt32(cboNationality.SelectedValue) == 32)//选中中国
                {
                    cboNation.IsEnabled = true;
                    //民族
                    var nations = myModel.D_AttributeDetails.Where(g => g.AttributeID == 7)
                        .OrderBy(g => g.AttributeDetailsID).ToList();
                    cboNation.ItemsSource = nations;
                    cboNation.DisplayMemberPath = "DetailsName";
                    cboNation.SelectedValuePath = "AttributeDetailsID";
                    cboNation.SelectedValue = dbPatient.A_NationID; ;//民族
                }
                else
                {
                    cboNation.ItemsSource = null;
                    cboNation.IsEnabled = false;
                }
            }
            catch (Exception)
            {
                EmptyControls();
                return;
            }
        }

4、效果示意图
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值