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; } }
Is there a good PropertyGrid implementation I could use in my applications?
This blog has a good PropertyGrid implementation: Your own WPF PropertyGrid control in a couple of hours
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'));
How do I filter out duplicate entries in my list when bound to a data source?
The fact that you have duplicate entries probably suggests that you are binding to the wrong data source. But, in any case, there are possibly valid scenarios where this is necessary. Here is a solution. One way is to group the list by name using CollectionViewSource and then bind the Groups to the ListBox. Here is some code: (for better code formatting, look at this thread: Distinct Value Filter For ListBox If you Contracts collection is like this: [C#] public class Contracts : List { public Contracts() { this.Add(new Contract() { ContractKey = '1', Name = 'John' }); this.Add(new Contract() { ContractKey = '2', Name = 'Bill' }); this.Add(new Contract() { ContractKey = '3', Name = 'Bill' }); } } public class Contract { public string ContractKey { get; set; } public string Name { get; set; } } Then you can use the CollectionViewSource to bind to it and group the colelction by Name as follows: [XAML] This will render the listbox the way you want – ignoring the duplicate names. You can then interpret the selected item as follows: [XAML] private void lb_SelectionChanged(object sender, SelectionChangedEventArgs e) { CollectionViewGroup groupSelected = ((CollectionViewGroup)e.AddedItems[0]); string name = groupSelected.Name; // These contain the items within the group //groupSelected.Items; }