internal void SetSelectedItem(ScatterViewItem item, bool value) { if (SelectedItem != null) { if (SelectedItem is DependencyObject) { SetIsSelected((DependencyObject)SelectedItem, false); ScatterViewItem.SetIsSelected((DependencyObject)SelectedItem, false); } else { DependencyObject dp = ItemContainerGenerator.ContainerFromItem(SelectedItem); SetIsSelected(dp, false); ScatterViewItem.SetIsSelected(dp, false); } } SetIsSelected(item, value); }
/// <summary> /// There is a flaw in the tab control where if all tabs are removed, then a new one is added, that single tab won't be selected (it will get /// selected when a second tab is added) /// /// This event listener fixes that flaw /// </summary> /// <remarks> /// http://stackoverflow.com/questions/1177006/how-to-make-sure-my-wpf-tabcontrol-always-has-a-selected-tab-when-it-contains-at /// </remarks> private void ItemContainerGenerator_StatusChanged(object sender, EventArgs e) { try { ItemContainerGenerator senderCast = sender as ItemContainerGenerator; if (senderCast == null || tabCtrl == null) { return; } if (senderCast.Status == System.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated && tabCtrl.SelectedIndex == -1) { tabCtrl.SelectedIndex = 0; } } catch (Exception ex) { MessageBox.Show(ex.ToString(), TITLE, MessageBoxButton.OK, MessageBoxImage.Error); } }
/// <summary> /// Updates the following based on the content of SelectedItems: /// - All SelectorItems "IsSelected" properties /// - Values refered to by SelectedMemberPath /// - SelectedItem property /// - SelectedValue property /// Refered to by the SelectedMemberPath property. /// </summary> private void UpdateFromSelectedItems() { foreach (object o in ItemsCollection) { bool isSelected = SelectedItems.Contains(o); _ignoreSelectedMemberPathValuesChanged++; this.SetSelectedMemberPathValue(o, isSelected); _ignoreSelectedMemberPathValuesChanged--; var selectorItem = ItemContainerGenerator.ContainerFromItem(o) as SelectorItem; if (selectorItem != null) { selectorItem.IsSelected = isSelected; } } UpdateSelectedItem(); UpdateSelectedValue(); }
private void BlurSubMenu() { var firstSubItem = ItemContainerGenerator.ContainerFromIndex(0); if (firstSubItem == null) { return; } var container = VisualTreeHelper.GetParent(firstSubItem) as Visual; if (container == null) { return; } DependencyObject parent = container; int borderIndex = 0; while (parent != null) { if (parent is Border b) { // only put color on the first border (transparent colors will add up otherwise) if (borderIndex == 0) { b.Background = new SolidColorBrush(Color.FromArgb(80, 0, 0, 0)); } else { b.Background = Brushes.Transparent; } borderIndex++; } parent = VisualTreeHelper.GetParent(parent); } AcrylicHelper.EnableBlur(container); }
/// <summary> /// Moves to the selected page, animating if a <see cref="PageTransition"/> is set. /// </summary> /// <param name="fromIndex">The index of the old page.</param> /// <param name="toIndex">The index of the new page.</param> /// <returns>A task tracking the animation.</returns> private async Task MoveToPage(int fromIndex, int toIndex) { if (fromIndex != toIndex) { var generator = ItemContainerGenerator; IControl from = null; IControl to = null; if (fromIndex != -1) { from = ItemContainerGenerator.ContainerFromIndex(fromIndex); } if (toIndex != -1) { to = GetOrCreateContainer(toIndex); } if (PageTransition != null && (from != null || to != null)) { await PageTransition.Start((Visual)from, (Visual)to, fromIndex < toIndex); } else if (to != null) { to.IsVisible = true; } if (from != null) { if (IsVirtualized) { Panel.Children.Remove(from); generator.Dematerialize(fromIndex, 1); } else { from.IsVisible = false; } } } }
private void OnTypeTreeListBoxStatusChanged(object sender, EventArgs e) { ItemContainerGenerator generator = (ItemContainerGenerator)sender; if (generator.Status == GeneratorStatus.ContainersGenerated) { for (int i = 0; i < generator.Items.Count; i++) { ListBoxItem listItem = (ListBoxItem)generator.ContainerFromIndex(i); if (listItem != null) { TreeNodeInfo info = (TreeNodeInfo)generator.Items[i]; Color expectedForegroundColor = GetForegroundStatusColor(info.Status); if (listItem.Foreground is SolidColorBrush solidForeBrush) { if (solidForeBrush.Color != expectedForegroundColor) { listItem.Foreground = new SolidColorBrush(expectedForegroundColor); } } else { listItem.Foreground = new SolidColorBrush(expectedForegroundColor); } Color expectedBackgroundColor = GetBackgroundStatusColor(info.Status); if (listItem.Background is SolidColorBrush solidBackBrush) { if (solidBackBrush.Color != expectedBackgroundColor) { listItem.Background = new SolidColorBrush(expectedBackgroundColor); } } else { listItem.Background = new SolidColorBrush(expectedBackgroundColor); } } } } }
private TreeViewItemEx FindItem(ItemContainerGenerator generator, object item) { TreeViewItemEx treeItem; if (DataContext is IProvidePath pathProvider) { var path = pathProvider.GetItemParents(item); for (int i = 0; i < path.Count; i++) { treeItem = (TreeViewItemEx)generator.ContainerFromItem(path[i]); treeItem.IsExpanded = true; generator = treeItem.ItemContainerGenerator; } } // This is a fallback that will fail if virtualized away treeItem = generator.ContainerFromItem(item) as TreeViewItemEx; if (treeItem != null) { return(treeItem); } foreach (object element in generator.Items) { treeItem = generator.ContainerFromItem(element) as TreeViewItemEx; if (treeItem == null) { continue; } treeItem = FindItem(treeItem.ItemContainerGenerator, item); if (treeItem != null) { return(treeItem); } } return(null); }
/// <summary> /// Moves to the selected page, animating if a <see cref="PageTransition"/> is set. /// </summary> /// <param name="fromIndex">The index of the old page.</param> /// <param name="toIndex">The index of the new page.</param> /// <returns>A task tracking the animation.</returns> private async Task MoveToPage(int fromIndex, int toIndex) { if (fromIndex != toIndex) { var generator = ItemContainerGenerator; IControl from = null; IControl to = null; if (fromIndex != -1) { from = ItemContainerGenerator.ContainerFromIndex(fromIndex); } if (toIndex != -1) { to = GetOrCreateContainer(toIndex); } if (PageTransition != null && (from != null || to != null)) { await PageTransition.Start((Visual)from, (Visual)to, fromIndex < toIndex, default);
private void UnselectAllInternal() { _ignoreSetSelectedValue = true; if (ItemsSource != null) { foreach (object item in ItemsSource) { var selectorItem = ItemContainerGenerator.ContainerFromItem(item) as SelectorItem; if (selectorItem != null) { if (selectorItem.IsSelected) { selectorItem.IsSelected = false; } } } } _ignoreSetSelectedValue = false; }
private void BringIndexIntoView(int idx) { if (RowsPanel == null || idx < 0 || idx >= Items.Count) { return; } _focusedRowIndex = idx; var frameworkElement = ItemContainerGenerator.ContainerFromIndex(_focusedRowIndex) as FrameworkElement; if (frameworkElement == null) { ItemContainerGenerator.StatusChanged += OnStatusChanged; RowsPanel.BringRowIntoView(_focusedRowIndex); } else if (frameworkElement != null) { frameworkElement.Focus(); frameworkElement.BringIntoView(); } }
public static TreeViewItem ContainerFromItemRecursive(this ItemContainerGenerator root, object item) { if (root.ContainerFromItem(item) is TreeViewItem treeViewItem) { return(treeViewItem); } foreach (var subItem in root.Items) { treeViewItem = root.ContainerFromItem(subItem) as TreeViewItem; var search = treeViewItem?.ItemContainerGenerator.ContainerFromItemRecursive(item); if (search != null) { return(search); } } return(null); }
/// <summary> /// Returns the tab item by using some kind of a hit-test /// </summary> /// <param name="offset">the absolute coordinate in pixels starting from the left</param> private TabItem GetItemByOffset(double offset) { var tabItems = Items.Cast <object>() .Select(item => ItemContainerGenerator.ContainerFromItem(item) as TabItem) .ToList(); double currentItemsWidth = 0; // get tabs one by one and calculate their aggregated width until the offset value is reached foreach (var ti in tabItems) { if (currentItemsWidth + ti.ActualWidth >= offset) { return(ti); } currentItemsWidth += ti.ActualWidth; } return(tabItems.LastOrDefault()); }
protected override void OnItemsChanged(NotifyCollectionChangedEventArgs e) { if (e.Action == NotifyCollectionChangedAction.Add || e.Action == NotifyCollectionChangedAction.Reset) { if (Items.Count > 1) { SelectedItem = Items[Items.Count - 1]; UpdateLayout(); ScrollIntoView(SelectedItem); } } else if (e.Action == NotifyCollectionChangedAction.Remove) { for (int i = 0; i < Items.Count; i++) { var item = ItemContainerGenerator.ContainerFromIndex(i) as ListBoxItem; var numberTB = VisualTreeHelper1.GetChildObject <TextBlock>(item, "NumberTB"); numberTB.Text = (i + 1).ToString(); } } }
protected override bool TryRemoveChildItemFromVisualTree(object item) { if (_useNativeComboBox) { if (_nativeComboBoxDomElement != null) { var optionDomElement = ItemContainerGenerator.INTERNAL_ContainerFromItem(item); if (optionDomElement != null) { INTERNAL_HtmlDomManager.RemoveOptionFromNativeComboBox(optionDomElement, _nativeComboBoxDomElement); return(ItemContainerGenerator.INTERNAL_TryUnregisterContainer(optionDomElement, item)); } } return(false); } else { return(base.TryRemoveChildItemFromVisualTree(item)); } }
public void TreeViewClickExecute(object obj) { if (null == obj) { return; } TreeView treeView = obj as TreeView; if (null == treeView) { return; } ItemCollection ic = treeView.Items; ItemContainerGenerator icg = treeView.ItemContainerGenerator; this.ClearTreeViewControlSelection(ic, icg); this.SelectedEntity = null; this.controlHost.unselect(); }
private void DoFocusItem() { Debug.Assert(focusItem != null); ContentPresenter contentPresenter = (ContentPresenter) ItemContainerGenerator.ContainerFromItem(focusItem); if (contentPresenter.IsLoaded) { Keyboard.Focus(WpfUtils.FindVisualChild <TextBox>(contentPresenter)); focusItem = null; } else { contentPresenter.Loaded += delegate(object obj, RoutedEventArgs e) { Keyboard.Focus(WpfUtils.FindVisualChild <TextBox>(contentPresenter)); focusItem = null; }; } }
private void ResetSeparators(bool reset = true) { if (reset) { for (var i = 0; i < Items.Count - 1; i++) { var container = ItemContainerGenerator.ContainerFromIndex(i) as WindowCommandsItem; if (container != null) { container.IsSeparatorVisible = ShowSeparators; } } } var lastContainer = ItemContainerGenerator.ContainerFromIndex(Items.Count - 1) as WindowCommandsItem; if (lastContainer != null) { lastContainer.IsSeparatorVisible = ShowSeparators && ShowLastSeparator; } }
internal new void ScrollIntoView(object item) { if (item == null) { return; } SelectedItem = item; base.ScrollIntoView(item, ColumnFromDisplayIndex(0)); Dispatcher.BeginInvoke(new Action(delegate { while (SelectedItem == null) { SelectedItem = item; } TreeListViewRow tlItem = ItemContainerGenerator.ContainerFromIndex(SelectedIndex) as TreeListViewRow; tlItem.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next)); })); }
private void FocusEditorForSelectedRow() { if (SelectedItem != null) { ItemContainerGenerator itemGenerator = ItemContainerGeneratorFor(SelectedItem); //BUG: Почему здесь иногда бывает null - не ясно if (itemGenerator != null) { TreeListViewItem li = itemGenerator.ContainerFromItem(SelectedItem) as TreeListViewItem; if (li != null) { MyEdit edit = li.GetEditor(GetViewLastEditorColumnId(), GetLastEditorColumnId(), false) as MyEdit; if (edit != null) { Keyboard.Focus(edit); } } } } }
//private static TreeViewItem ContainerFromItem(ItemContainerGenerator root, object item) //{ // if (root.ContainerFromItem(item) is TreeViewItem treeViewItem) // { // return treeViewItem; // } // return null; //} private static TreeViewItem ContainerFromItemRecursive(ItemContainerGenerator root, object item) { if (root.ContainerFromItem(item) is TreeViewItem treeViewItem) { return(treeViewItem); } foreach (var subItem in root.Items) { treeViewItem = root.ContainerFromItem(subItem) as TreeViewItem; if (treeViewItem != null) { var search = ContainerFromItemRecursive(treeViewItem.ItemContainerGenerator, item); if (search != null) { return(search); } } } return(null); }
/// <summary> /// Tries to set the selection to a container that raised an event. /// </summary> /// <param name="eventSource">The control that raised the event.</param> /// <param name="select">Whether the container should be selected or unselected.</param> private void TrySetSelectionFromContainerEvent(IInteractive eventSource, bool select) { var item = GetContainerFromEvent(eventSource); if (item != null) { var index = ItemContainerGenerator.IndexFromContainer(item); if (index != -1) { if (select) { SelectedIndex = index; } else { LostSelection(); } } } }
protected override Size ArrangeOverride(Size finalSize) { for (var childIndex = 0; childIndex < InternalChildren.Count; ++childIndex) { var internalChild = InternalChildren[childIndex]; var indexFromChildIndex = ItemContainerGenerator.IndexFromGeneratorPosition(new GeneratorPosition(childIndex, 0)); var columnIndex = indexFromChildIndex % _itemsPerRow; var rowIndex = indexFromChildIndex / _itemsPerRow; var itemX = columnIndex * _itemSize.Width; var itemY = rowIndex * _itemSize.Height; var childItemRect = new Rect(itemX, itemY, _itemSize.Width, _itemSize.Height); internalChild.Arrange(childItemRect); } return(finalSize); }
/// <summary> /// A version of ContainerFromItem that works with HierarchicalDataTemplate /// </summary> /// <param name="containerGenerator">The container generator.</param> /// <param name="item">The item you're looking for.</param> /// <typeparam name="T">The type of items control (usually a TreeViewItem).</typeparam> /// <returns>The container object.</returns> public static T ContainerFromItemRecursive <T>(this ItemContainerGenerator containerGenerator, object item) where T : ItemsControl { if (containerGenerator.ContainerFromItem(item) is T container) { return(container); } foreach (var subItem in containerGenerator.Items) { if (containerGenerator.ContainerFromItem(subItem) is ItemsControl itemsControl) { container = itemsControl.ItemContainerGenerator.ContainerFromItemRecursive <T>(item); if (container != null) { return(container); } } } return(null); }
private TreeViewItem GetTreeViewItem(ItemContainerGenerator generator, OutlineItem outlineItem) { var retrievedItem = generator.ContainerFromItem(outlineItem); if (retrievedItem != null) { return((TreeViewItem)retrievedItem); } for (int i = 0; i < generator.Items.Count; i++) { TreeViewItem childItem = (TreeViewItem)generator.ContainerFromIndex(i); retrievedItem = GetTreeViewItem(childItem.ItemContainerGenerator, outlineItem); if (retrievedItem != null) { return((TreeViewItem)retrievedItem); } } return(null); }
private TreeViewItem GetSelectedItem(ItemContainerGenerator icg, QueryItem newQueryItem) { int i = 0; while (true) { TreeViewItem item = (TreeViewItem)icg.ContainerFromIndex(i++); if (item == null) { return(null); } if (item.Header.ToString().Equals(newQueryItem.Path, StringComparison.OrdinalIgnoreCase)) { return(item); } if (newQueryItem.Path.StartsWith(item.Header.ToString(), StringComparison.OrdinalIgnoreCase)) { return(GetSelectedItem(item.ItemContainerGenerator, newQueryItem)); } } }
private bool ContainSelection() { if (!HasItems) { return(false); } for (int i = 0; i < Items.Count; i++) { var container = ItemContainerGenerator.ContainerFromIndex(i) as MultiSelectTreeViewItem; if (container != null) { if (container.IsSelected || container.ContainSelection()) { return(true); } } } return(false); }
private void selectionChanged(object sender, SelectionChangedEventArgs e) { blur = new BlurEffect(); blur.Radius = 20; listView.Effect = blur; ItemContainerGenerator generator = this.listView.ItemContainerGenerator; ListBoxItem selectedItem = (ListBoxItem)generator.ContainerFromIndex(listView.SelectedIndex); var id = "index" + listView.SelectedIndex.ToString(); Label aLabel = GetChildrenByType(selectedItem, typeof(Label), id) as Label; if (aLabel != null) { widgetGrid.Visibility = Visibility.Visible; nameLabel.Content = aLabel.Content; itemNumber.Content = 1.ToString(); } else { MessageBox.Show("Error"); } }
private void SubItemsMaxDepth(ref int subtreeDepth) { if (!HasItems) { subtreeDepth = 0; } else { for (int i = 0; i < Items.Count; i++) { var curMaxDepth = 0; var container = ItemContainerGenerator.ContainerFromIndex(i) as MultiSelectTreeViewItem; container.SubItemsMaxDepth(ref curMaxDepth); subtreeDepth = Math.Max(subtreeDepth, curMaxDepth); } subtreeDepth++; } }
/// <summary> /// Called when a container raises the <see cref="IsSelectedChangedEvent"/>. /// </summary> /// <param name="e">The event.</param> private void ContainerSelectionChanged(RoutedEventArgs e) { if (!_ignoreContainerSelectionChanged) { var control = e.Source as IControl; var selectable = e.Source as ISelectable; if (control != null && selectable != null && control.LogicalParent == this && ItemContainerGenerator?.IndexFromContainer(control) != -1) { UpdateSelection(control, selectable.IsSelected); } } if (e.Source != this) { e.Handled = true; } }
void ExpandAll(ItemsControl items, bool expand) { ItemContainerGenerator itemContainerGenerator = items.ItemContainerGenerator; for (int i = items.Items.Count - 1; i >= 0; --i) { ItemsControl childControl = itemContainerGenerator.ContainerFromIndex(i) as ItemsControl; if (childControl != null) { ExpandAll(childControl, expand); } } TreeViewItem treeViewItem = items as TreeViewItem; if (treeViewItem != null) { treeViewItem.IsExpanded = expand; } }