How do I access the ItemsPresenter of a ListBox?

This same logic can be used to access the ItemsPresenter of other ItemsControls like ListView, etc. [C#] public partial class RetrieveItemsPanelDemo : Window { public RetrieveItemsPanelDemo() { InitializeComponent(); ListBox listBox = new ListBox() { ItemsSource = Enumerable.Range(0, 10) }; this.Content = listBox; listBox.Loaded += delegate { VirtualizingStackPanel itemsPanel = listBox.GetVisualChild(); if (itemsPanel != null && itemsPanel.IsItemsHost) { itemsPanel.PreviewMouseLeftButtonDown += delegate { MessageBox.Show(‘WPF’); }; } }; } } public static T GetVisualChild(this Visual referenceVisual) where T : Visual { Visual child = null; for (Int32 i = 0; i < VisualTreeHelper.GetChildrenCount(referenceVisual); i++) { child = VisualTreeHelper.GetChild(referenceVisual, i) as Visual; if (child != null && (child.GetType() == typeof(T))) { break; } else if (child != null) { child = GetVisualChild(child); if (child != null && (child.GetType() == typeof(T))) { break; } } } return child as T; }

What is the equivalent of the Windows Forms OnIdle event in WPF?

In WPF, you can set something to be run on application idle or system idle using the Dispatcher instance of the UI thread as follows: [C#] // First declare the delgate. public delegate void OneArgDelegate(int weather); // This is the method that will be called on system idle: public void UpdateWeather(int weather) { … } // Schedule the above method for execution in the UI thread on system idle. tomorrowsWeather.Dispatcher.BeginInvoke( System.Windows.Threading.DispatcherPriority.SystemIdle, new OneArgDelegate(UpdateUserInterface), weather); The other dispatcher priority options that are available are: SystemIdle, ApplicationIdle, ContextIdle, Background, Input, Loaded, Render, DataBind, Normal, Send

How do I access the ui element at a row/cell in my GridView?

The following utility class (provided in this thread: Accessing and Mondifying DataTemplate Element Runtime for CellTemplate should help you achieve this: [C#] public static class ListViewHelper { public static FrameworkElement GetElementFromCellTemplate(ListView listView, Int32 column, Int32 row, String name) { if (row >= listView.Items.Count || row < 0) { throw new ArgumentOutOfRangeException(‘row’); } GridView gridView = listView.View as GridView; if (gridView == null) { return null; } if (column >= gridView.Columns.Count || column < 0) { throw new ArgumentOutOfRangeException(‘column’); } ListViewItem item = listView.ItemContainerGenerator.ContainerFromItem(listView.Items[row]) as ListViewItem; if (item != null) { GridViewRowPresenter rowPresenter = GetFrameworkElementByName(item); if (rowPresenter != null) { ContentPresenter templatedParent = VisualTreeHelper.GetChild(rowPresenter, column) as ContentPresenter; DataTemplate dataTemplate = gridView.Columns[column].CellTemplate; if (dataTemplate != null && templatedParent != null) { return dataTemplate.FindName(name, templatedParent) as FrameworkElement; } } } return null; } private static T GetFrameworkElementByName(FrameworkElement referenceElement) where T : FrameworkElement { FrameworkElement child = null; for (Int32 i = 0; i < VisualTreeHelper.GetChildrenCount(referenceElement); i++) { child = VisualTreeHelper.GetChild(referenceElement, i) as FrameworkElement; System.Diagnostics.Debug.WriteLine(child); if (child != null && child.GetType() == typeof(T)) { break; } else if (child != null) { child = GetFrameworkElementByName(child); if (child != null && child.GetType() == typeof(T)) { break; } } } return child as T; } }

How do I create a template programatically?

You can create a template programatically by populating the VisualTree of the template. [C#] DataTemplate template = new DataTemplate(); template.VisualTree = new FrameworkElementFactory(typeof(Path)); template.VisualTree.SetBinding(Path.StrokeProperty, new Binding(‘Stroke’)); template.VisualTree.SetBinding(Path.StrokeThicknessProperty, new Binding(‘StrokeThickness’)); template.VisualTree.SetBinding(Path.FillProperty, new Binding(‘Interior’)); template.VisualTree.SetBinding(Path.DataProperty, new Binding(‘Geometry’));