How do I capture Ctrl + Enter key press in my TextBox?

You can do so by listening to the ComponentDispatcher.ThreadPreprocessMessage in your window as follows: [C#] using System; using System.Windows; using System.Windows.Controls; using swf = System.Windows.Forms; using System.Windows.Interop; using System.Windows.Forms.Integration; using System.Windows.Input; namespace WpfTestHarness { public partial class CustomShortcutHandlingDemo : Window { public CustomShortcutHandlingDemo() { InitializeComponent(); // Registering against a stack event will cause memory leak, please unregister this event when you are done with it. ComponentDispatcher.ThreadPreprocessMessage += ComponentDispatcher_ThreadPreprocessMessage; } const int WM_KEYDOWN = 0x100; const int WM_SYSKEYDOWN = 0x0104; private void ComponentDispatcher_ThreadPreprocessMessage(ref MSG msg, ref bool handled) { if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN) { // Examine if the Control and Enter keys are pressed, we also need to make sure that the // currently keyborad focused element is TextBox instance itself. swf.Keys keyData = ((swf.Keys)((int)((long)msg.wParam))) | swf.Control.ModifierKeys; if (((keyData & swf.Keys.Control) == swf.Keys.Control) && ((keyData & swf.Keys.Enter) == swf.Keys.Enter) && Keyboard.FocusedElement == textBox) { MessageBox.Show(‘Ctrl+Enter is pressed’); } } } } }

How do I change the background of my Grid layout when one of it’s children gets keyboard focus?

You can change the background of the grid layout when one of its children gets keyboard focus by setting the IsKeyboardFocusWithin property. This can be done with the following code snippet, [XAML] <Window.Resources> <Style x:Key = “GridTriggerStyle” TargetType = “Grid”> <Style.Triggers> <Trigger Property = “IsKeyboardFocusWithin” Value = “True”> <Setter Property = “Background” Value = “Green” /> </Trigger> <Trigger Property = “IsKeyboardFocusWithin” Value = “False”> <Setter Property = “Background” Value = “AliceBlue” /> </Trigger> </Style.Triggers> </Style> </Window.Resources> <Grid Name=”testGrid” Style=”{StaticResource GridTriggerStyle}”> <Button Height=”30″ Width=”100″ Content=”SampleButton1″/> </Grid>

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; } }