70895

WPF Get Item of Itemscontrol in Visualtree

Question:

I am implementing an DragAndDrop-manager for wpf using attached properties. It works quite nice. But there is only one problem. To grab the dragged item i am using the visualtree. As example I want to have the listboxitem but the originalsource is the border of the listboxitem. So I just use one of my helper methods to search for the parent with the type of ListBoxItem. If I found that I get the data of it and drag that.

But I dont want to have my DragAndDrop-manager aviable only while using a listbox. No I want to use it on every Itemscontrol. But a DataGrid uses DataGridRows, a listview uses ListViewItem... So is there any chance to get the item without writing the code again, again and again?

Answer1:

well, you can have this function (i prefer to have it as static):

public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject { if (depObj != null) { for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++) { DependencyObject child = VisualTreeHelper.GetChild(depObj, i); if (child != null && child is T) { yield return (T)child; } foreach (T childOfChild in FindVisualChildren<T>(child)) { yield return childOfChild; } } } }

and use it some kind of this:<br /> i.e. you want to find all TextBox elements in yourDependencyObjectToSearchIn container

foreach (TextBox txtChild in FindVisualChildren<TextBox>(yourDependencyObjectToSearchIn)) { // do whatever you want with child of type you were looking for // for example: txtChild.IsReadOnly = true; }

if you want me to provide you some explanation, i'll do this as soon as i get up)

Answer2:

You can use FrameworkElement or UIElement to identify the control.

Control inheritance hierarchy..

System.Object

System.Windows.Threading.DispatcherObject

System.Windows.DependencyObject System.Windows.Media.Visual System.Windows.UIElement System.Windows.**FrameworkElement** System.Windows.Controls.Control System.Windows.Controls.ContentControl System.Windows.Controls.ListBoxItem System.Windows.Controls.**ListViewItem**

System.Object

System.Windows.Threading.DispatcherObject

System.Windows.DependencyObject System.Windows.Media.Visual System.Windows.UIElement System.Windows.**FrameworkElement** System.Windows.Controls.Control System.Windows.Controls.**DataGridRow**

Recommend

  • Access property of DataContext inside ItemTemplate
  • ListBox Selected Item
  • In metro, what's the difference between FolderInformation and StorageFolder
  • InfoWindow showing in wrong place
  • Embedding an existing UINavigationController to new view controllers
  • how to add horizontal scroll bar to a panel In ExtJs?
  • Initial DataGrid Sorting
  • Bind customObject to DataGrid
  • Use Ellipse with origin in the center WPF
  • View Injection inside ItemsControl
  • Scroller-like animation on a Label in WPF
  • A ListBoxItem that fills its parent
  • Calling makeViewWithIdentifier:owner: causes ARC to re-create ivar
  • Is DataGrid an alternative for UWP?
  • auto layout leading or trainling -16
  • global.html is unable to load NPAPI plugin from safari-extension builder but its loading from the di
  • Nested INotifyPropertyChanged class won't work
  • jQueryUI Draggable: Constrain draggability to a single axis?
  • how to use view.setX() method in android version less then 3.0
  • Adding predefined item to a ComboBox with ItemsSource
  • Programmatically setting the record pointer in a C# DataGridView
  • Which way to go with graphic-intense multi-touch app, XNA or WPF?
  • Showing a concatenated string from multiple values of observable array
  • Button inside Update Panel is not triggered, in asp.net
  • JQuery UI selectable plugin - Multiple mouse drag selection and unselect option
  • Operation not supported on read-only collection
  • Change color of row programmatically in WatchKit
  • Can't CTRL+Drag NSButton to custom NSView header
  • How to prompt user that edits have been made upon changing pages or sorting in Kendo Grid
  • Binding a list with another list with WPF
  • Fancy dynamic list in Android: TableLayout vs ListView
  • React Native fetch is not a function
  • jquery draggable stop event
  • UWP change ComboBox position when opening
  • How to create a data template dependent on an XML Attribute?
  • Bind collection of objects to ListBox
  • WPF version of .ScaleControl?
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • How can I use `wmic` in a Windows PE script?
  • Unable to use reactive element in my shiny app