/// <summary> /// Handles the DragDrop event. /// Determines the target and fires the corresponding action (move or copy). /// </summary> /// <param name="source">The source of the event</param> /// <param name="e">The event arguments</param> private void HandleDragDrop(object source, DragDropEventArgs e) { if (e.Effect != DragDropEffect.None) { // make the move... C1TreeViewItem item = e.DragSource as C1TreeViewItem; if (item != null) { // find target node C1TreeViewItem target = Tree.GetNode(e.GetPosition(null)); if (target != null) { bool insertAfter = e.GetPosition(target).Y > 4; if (target.HasItems) { // if target is department, insert new item inside target = target.FirstNode; insertAfter = false; } if (Effect == DragDropEffect.Move) { DoMove(item, target, insertAfter); } else { DoCopy(item, target, insertAfter); } Tree.ItemsSource = Tree.ItemsSource; } } } }
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { if (value == null) { throw new ArgumentNullException("value"); } C1TreeViewItem itemValue = value as C1TreeViewItem; if ((itemValue != null) && (itemValue.Header != null)) { var group = itemValue.Header as WorldCupGroup; var team = itemValue.Header as WorldCupTeam; if (group != null) { return(group.GroupName); } if (team != null) { return(team.CountryName); } } return(value.ToString()); }
private void TreeView_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (e.RemovedItems.Count() > 0 && (e.RemovedItems[0] as C1TreeViewItem).DataContext is SubCategory) { PrevSelecteditem = e.RemovedItems[0] as C1TreeViewItem; } }
private void ClearSelectedSubCategoryTreeViewItem(C1TreeViewItem item) { StackPanel stackPanel = (FindVisualChild <StackPanel>(item) as StackPanel); stackPanel.Background = new SolidColorBrush(Windows.UI.Colors.Transparent); TextBlock textBlock = (FindVisualChild <TextBlock>(item) as TextBlock); textBlock.Foreground = new SolidColorBrush(Windows.UI.Colors.Gray); }
private void SetPropertiesForSelectedItem(C1TreeViewItem clickedItem) { this.gridChoice.Visibility = Visibility.Visible; _flexItem.Visibility = Visibility.Visible; _flexOrder.Visibility = Visibility.Collapsed; try { StackPanel stackPanel; TextBlock textBlock; C1TreeViewItem item; if (clickedItem.DataContext is Category) { SelectedCategory = clickedItem.DataContext as Category; item = (C1TreeViewItem)clickedItem; stackPanel = (FindVisualChild <StackPanel>(item) as StackPanel); Path path = stackPanel.FindName("CollapsedIcon") as Path; if (path.Visibility == Visibility.Collapsed) { path.Visibility = Visibility.Visible; (stackPanel.FindName("ExpandedIcon") as Path).Visibility = Visibility.Collapsed; } else { path.Visibility = Visibility.Collapsed; (stackPanel.FindName("ExpandedIcon") as Path).Visibility = Visibility.Visible; } } else { if (PrevSelecteditem != null) { ClearSelectedSubCategoryTreeViewItem(PrevSelecteditem); } SubCategory subCategory = SelectedSubCategory = (clickedItem.DataContext as SubCategory); item = clickedItem; stackPanel = (FindVisualChild <StackPanel>(item) as StackPanel); stackPanel.Background = new SolidColorBrush(Windows.UI.Colors.Green); textBlock = (FindVisualChild <TextBlock>(item) as TextBlock); textBlock.Foreground = new SolidColorBrush(Windows.UI.Colors.White); txtSubCategory.Text = SelectedSubCategory.Text; if (SelectedSubCategory.Name == "All") { SelectedCategory = _categories.Where(c => c.Name == SelectedSubCategory.CategoryName).FirstOrDefault(); txtSubCategory.Text = SelectedCategory.Text; AddItemsInGridBasedOnSelectedCategory(); } else { AddItemsInGridBasedOnSelectedSubCategory(SelectedSubCategory, true); } } } catch (Exception) { } }
/// <summary> /// Copies a node to the same collection the target node belongs to. /// </summary> /// <param name="source">The source node to copy</param> /// <param name="target">The target node</param> /// <param name="copyAfter">Indicates if the source node will be copied after /// (larger index) the target </param> private void DoCopy(C1TreeViewItem source, C1TreeViewItem target, bool copyAfter) { int targetIndex = target.Index; IList targetCollection = (IList)target.ParentItemsSource; // adjust target index if necessary (insert after or before the target) if (copyAfter) { targetIndex++; } var sourceTreeViewItem = source as C1TreeViewItem; var targetTreeViewItem = target as C1TreeViewItem; var empSource = sourceTreeViewItem.DataContext as Employee; var departmentId = 0; if (targetTreeViewItem.DataContext is Employee) { departmentId = ((Employee)targetTreeViewItem.DataContext).DepartmentID; } else { departmentId = ((Department)targetTreeViewItem.DataContext).DepartmentID; } if (departmentId == 0 || empSource.DepartmentID == departmentId) { return; } foreach (var dep in Tree.ItemsSource) { var department = dep as Department; if (department.DepartmentID != departmentId) { continue; } var empToAdd = empSource.Clone(); empToAdd.DepartmentID = departmentId; if (!department.Employees.Any(x => x.EmployeeID == empToAdd.EmployeeID)) { if (!department.Employees.Any()) { department.Employees.Add(empToAdd); } else { department.Employees.Insert(targetIndex, empToAdd); } } break; } }
private void ExpandCollapseTreeView(Categories categories) { foreach (Reports report in categories.ReportsList) { C1TreeViewItem item = (C1TreeViewItem)myTreeView.ItemContainerGenerator.ContainerFromItem(report); if (item == null) { return; } item.Visibility = (item.Visibility == Visibility.Collapsed) ? Visibility.Visible : Visibility.Collapsed; } }
private void FillTree() { foreach (Categories category in _list) { myTreeView.Items.Add(category); foreach (Reports report in category.ReportsList) { myTreeView.Items.Add(report); C1TreeViewItem item = (C1TreeViewItem)myTreeView.ItemContainerGenerator.ContainerFromItem(report); if (item == null) { return; } item.Visibility = Visibility.Collapsed; } } }
void AddPersonToTree(Person p, ItemCollection items) { // create an item for this person var item = new C1TreeViewItem(); item.Tag = p; item.Header = p.Name; // add this person to the tree items.Add(item); // and add any children foreach (var child in p.Children) { AddPersonToTree(child, item.Items); } }
private void TreeViewItem_Loaded(object sender, RoutedEventArgs e) { C1TreeViewItem treeViewItem = (C1TreeViewItem)sender; if (treeViewItem.DataContext is Category && (treeViewItem.DataContext as Category).Name == SelectedCategory.Name) { treeViewItem.EnsureVisible(); treeViewItem.IsSelected = true; treeViewItem.IsExpanded = true; SetPropertiesForSelectedItem(treeViewItem); } if (PrevSelecteditem == null && treeViewItem.DataContext is SubCategory && (treeViewItem.DataContext as SubCategory).Name == SelectedCategory.SubCategories[0].Name) { treeViewItem.EnsureVisible(); treeViewItem.IsSelected = true; SetPropertiesForSelectedItem(treeViewItem); } }
private void MyTreeView_Loaded(object sender, RoutedEventArgs e) { if (!myTreeView.HasItems) { FillTree(); //Select and open first report foreach (Categories category in _list) { Reports rptSelected = category.ReportsList.Where(r => r.IsSelected == true).FirstOrDefault(); if (rptSelected != null) { C1TreeViewItem itemReport = (C1TreeViewItem)myTreeView.ItemContainerGenerator.ContainerFromItem(rptSelected); ExpandCollapseTreeView(category); category.ExpenderImg = @"Resources/collapse.png"; itemReport.IsSelected = true; OpenFile(rptSelected.FileName, rptSelected.RptName); break; } } } }
private void FlexGrid_Drop(object sender, DragEventArgs e) { C1FlexGrid grid = sender as C1FlexGrid; if (grid != null) { if (e.Data.GetDataPresent(typeof(C1TreeViewItem))) { C1TreeViewItem item = (C1TreeViewItem)e.Data.GetData(typeof(C1TreeViewItem)); Random Randomizer = new Random(); _flexGridData.Add(new Person() { Name = item.Header.ToString(), Address = Person.Streets[Randomizer.Next(Person.Streets.Length)] + " " + (1000 + Randomizer.Next(8999)).ToString(), Age = 30 + Randomizer.Next(10), Residence = Person.Countries[Randomizer.Next(Person.Countries.Length)], Born = Person.Countries[Randomizer.Next(Person.Countries.Length)], }); treeView.Items.Remove(item); } } }
/// <summary> /// Copies a node to the same collection the target node belongs to. /// </summary> /// <param name="source">The source node to copy</param> /// <param name="target">The target node</param> /// <param name="copyAfter">Indicates if the source node will be copied after /// (larger index) the target </param> private void DoCopy(C1TreeViewItem source, C1TreeViewItem target, bool copyAfter) { int targetIndex = target.Index; IList targetCollection = (IList)target.ParentItemsSource; // adjust target index if necessary (insert after or before the target) if (copyAfter) targetIndex++; // copy source node and insert Employee sourceCopy = ((Employee)source.Header).Clone(); targetCollection.Insert(targetIndex, sourceCopy); }
/// <summary> /// Moves a node to the same collection the target node belongs to. /// </summary> /// <param name="source">The source node to move</param> /// <param name="target">The target node</param> /// <param name="moveAfter">Indicates if the source node will be moved after (larger index) the target </param> private void DoMove(C1TreeViewItem source, C1TreeViewItem target, bool moveAfter) { // get source collection/index int sourceIndex = source.Index; int targetIndex = target.Index; IList sourceCollection = (IList)source.ParentItemsSource; IList targetCollection = (IList)target.ParentItemsSource; // adjust target index if necessary (insert after or before the target) if (moveAfter) { targetIndex++; } if (sourceCollection == targetCollection && sourceIndex < targetIndex) { targetIndex--; } var sourceTreeViewItem = source as C1TreeViewItem; var targetTreeViewItem = target as C1TreeViewItem; var empSource = sourceTreeViewItem.DataContext as Employee; var departmentId = 0; if (targetTreeViewItem.DataContext is Employee) { departmentId = ((Employee)targetTreeViewItem.DataContext).DepartmentID; } else { departmentId = ((Department)targetTreeViewItem.DataContext).DepartmentID; } if (departmentId == 0 || empSource.DepartmentID == departmentId) { return; } foreach (var dep in Tree.ItemsSource) { var department = dep as Department; if (department.DepartmentID != departmentId) { continue; } if (!department.Employees.Any(x => x.EmployeeID == empSource.EmployeeID)) { if (!department.Employees.Any()) { department.Employees.Add(empSource); } else { department.Employees.Insert(targetIndex, empSource); } } break; } // remove node from old position if (sourceIndex >= 0 && empSource.DepartmentID != departmentId) { empSource.DepartmentID = departmentId; sourceCollection.RemoveAt(sourceIndex); } }
/// <summary> /// Moves a node to the same collection the target node belongs to. /// </summary> /// <param name="source">The source node to move</param> /// <param name="target">The target node</param> /// <param name="copyAfter">Indicates if the source node will be moved after /// (larger index) the target </param> private void DoMove(C1TreeViewItem source, C1TreeViewItem target, bool moveAfter) { // get source collection/index int sourceIndex = source.Index; int targetIndex = target.Index; IList sourceCollection = (IList)source.ParentItemsSource; IList targetCollection = (IList)target.ParentItemsSource; // adjust target index if necessary (insert after or before the target) if (moveAfter) targetIndex++; if (sourceCollection == targetCollection && sourceIndex < targetIndex) targetIndex--; // remove node from old position, insert into new sourceCollection.RemoveAt(sourceIndex); targetCollection.Insert(targetIndex, source.Header); }
/// <summary> /// Returns true if the target node is a valid drop location. /// For this sample, a valid target node is the one that: /// Represents an employee (departments are not valid) /// Is not being copied to the same department it is assigned /// Is not being moved to a department it is already assigned /// </summary> /// <param name="source"></param> /// <param name="target"></param> /// <returns></returns> private bool IsValidDrop(C1TreeViewItem source, C1TreeViewItem target) { bool isValidTarget = false; // For this sample we will allow dragging of employees only if ((source != null) && (target != null) && (source.Header is Employee) && (target.Header is Employee)) { // Cannot copy employees in the same department Employee sourceDataObject = (Employee)source.Header; bool isAlreadyInDepartment = ((IList)target.ParentItemsSource).Contains(sourceDataObject); if ((isAlreadyInDepartment) && ((Effect == DragDropEffect.Copy) || (source.Parent != target.Parent))) { isValidTarget = false; } else { isValidTarget = (!source.IsAncestorOf(target) && !(source == target)); } } return isValidTarget; }
private void TreeView_ItemPrepared(object sender, ItemPreparedEventArgs e) { C1TreeViewItem treeViewItem = (e.Element as C1TreeViewItem); treeViewItem.Loaded += TreeViewItem_Loaded; }