/// <summary> /// 选中TVI /// </summary> /// <param name="control"></param> /// <param name="dr"></param> private void SelectTreeViewItem(ItemsControl control, DataRow dr) { if (control != null) { foreach (object item in control.Items) { TreeViewItem treeItem = control.ItemContainerGenerator.ContainerFromItem(item) as TreeViewItem; if (treeItem != null) { PropertyNodeItem pni = treeItem.DataContext as PropertyNodeItem; if (pni.ID == Convert.ToInt32(dr["ID"])) { treeItem.IsSelected = true; return; } else if (treeItem.HasItems) { if (treeItem.ItemContainerGenerator.Status != System.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated) { treeItem.UpdateLayout(); } SelectTreeViewItem(treeItem as ItemsControl, dr); } } } } }
/// <summary> /// /选中DGR /// </summary> /// <param name="pni"></param> /// <param name="check"></param> /// <param name="visibile"></param> private void SelectDataGridRow(PropertyNodeItem pni, bool?check, Visibility visibile) { for (int i = 0; i < datagrid.ItemContainerGenerator.Items.Count; i++) { DataGridRow dgv = (DataGridRow)datagrid.ItemContainerGenerator.ContainerFromIndex(i); if (dgv == null) { datagrid.UpdateLayout(); datagrid.ScrollIntoView(datagrid.Items[i]); dgv = (DataGridRow)datagrid.ItemContainerGenerator.ContainerFromIndex(i); } DataRow dr = (dgv.Item as DataRowView).Row; if (pni.ID == Convert.ToInt32(dr["ID"])) { dgv.Visibility = visibile; if (visibile == Visibility.Visible) { if (check == true) { dgv.IsSelected = true; } else if (check == false) { dgv.IsSelected = false; } } } } }
/// <summary> /// 节点展开 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void OnLoaded(object sender, RoutedEventArgs e) { TreeViewItem tvi = sender as TreeViewItem; PropertyNodeItem pni = tvi.DataContext as PropertyNodeItem; SelectDataGridRow(pni, null, pni.Visibility); e.Handled = true; }
/// <summary> /// TVI可视状态改变 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void TreeItem_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e) { TreeViewItem tvi = sender as TreeViewItem; PropertyNodeItem pni = tvi.DataContext as PropertyNodeItem; if (tvi.IsVisible) { SelectDataGridRow(pni, null, Visibility.Visible); } else { SelectDataGridRow(pni, null, Visibility.Collapsed); } }
/// <summary> /// 被选择要选中对应的项 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void OnSelected(object sender, RoutedEventArgs e) { if (sender.GetType() == typeof(TreeViewItem)) { TreeViewItem tvi = sender as TreeViewItem; PropertyNodeItem pni = tvi.DataContext as PropertyNodeItem; SelectDataGridRow(pni, tvi.IsSelected, Visibility.Visible); e.Handled = true; } else if (sender.GetType() == typeof(DataGridRow)) { DataGridRow dgv = sender as DataGridRow; DataRow dr = (dgv.Item as DataRowView).Row; SelectTreeViewItem(treeview, dr); } }
/// <summary> /// 读取树数据 /// </summary> private void ShowTreeView() { List <PropertyNodeItem> itemList = new List <PropertyNodeItem>(); PropertyNodeItem node1 = new PropertyNodeItem() { ID = 1, FatherID = 0, DisplayName = "Node No.1", Name = "This is the discription of Node1. This is a folder.", IsExpanded = false }; PropertyNodeItem node1tag1 = new PropertyNodeItem() { ID = 2, FatherID = 1, DisplayName = "Tag No.1", Name = "This is the discription of Tag 1. This is a tag.", IsExpanded = false }; node1.Children.Add(node1tag1); PropertyNodeItem node1tag2 = new PropertyNodeItem() { ID = 3, FatherID = 1, DisplayName = "Tag No.2", Name = "This is the discription of Tag 2. This is a tag.", IsExpanded = false }; node1.Children.Add(node1tag2); itemList.Add(node1); PropertyNodeItem node2 = new PropertyNodeItem() { ID = 4, FatherID = 0, DisplayName = "Node No.2", Name = "This is the discription of Node 2. This is a folder.", IsExpanded = false }; PropertyNodeItem node2tag3 = new PropertyNodeItem() { ID = 5, FatherID = 4, DisplayName = "Tag No.3", Name = "This is the discription of Tag 3. This is a tag.", IsExpanded = false }; node2.Children.Add(node2tag3); PropertyNodeItem node2tag4 = new PropertyNodeItem() { ID = 6, FatherID = 4, DisplayName = "Tag No.4", Name = "This is the discription of Tag 4. This is a tag.", IsExpanded = false }; node2.Children.Add(node2tag4); itemList.Add(node2); PropertyNodeItem node1tag1tag1 = new PropertyNodeItem() { ID = 7, FatherID = 2, DisplayName = "TagTag No.1", Name = "This is the discription of TagTag 1. This is a tag.", IsExpanded = false }; node1tag1.Children.Add(node1tag1tag1); PropertyNodeItem node1tag1tag2 = new PropertyNodeItem() { ID = 8, FatherID = 2, DisplayName = "TagTag No.2", Name = "This is the discription of TagTag 2. This is a tag.", IsExpanded = false }; node1tag1.Children.Add(node1tag1tag2); PropertyNodeItem node1tag1tag1tag1 = new PropertyNodeItem() { ID = 9, FatherID = 7, DisplayName = "TagTagTag No.1", Name = "This is the discription of TagTagTag 1. This is a tag.", IsExpanded = false }; node1tag1tag1.Children.Add(node1tag1tag1tag1); PropertyNodeItem node1tag1tag1tag2 = new PropertyNodeItem() { ID = 10, FatherID = 7, DisplayName = "TagTagTag No.2", Name = "This is the discription of TagTagTag 2. This is a tag.", IsExpanded = false }; node1tag1tag1.Children.Add(node1tag1tag1tag2); this.treeview.ItemsSource = itemList; }