Syncfusion WPF FAQ
Questions and answers in this FAQ have been collected from newsgroup posts, various mailing lists and the employees of Syncfusion.

9. Concepts Input and Commands.

FAQ Home
   9.1 What built-in commands are defined in the WPF framework?
   9.2 How can I add a CommandBinding to a window using markup?
   9.3 How do I capture Ctrl + Enter key press in my TextBox?
   9.4 How do I change the background of my Grid layout when one of it's children gets keyboard focus?



9.1 What built-in commands are defined in the WPF framework?


WPF provides a library of common commands, which include

Sample code given below:

[XAML]

<Window x:Class="SDKSamples.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MenuItemCommandTask">
<DockPanel>
<Menu DockPanel.Dock="Top">
<MenuItem Command="ApplicationCommands.Paste" Width="75" />
</Menu>
<TextBox BorderBrush="Black" BorderThickness="2" Margin="25"
TextWrapping="Wrap">
The MenuItem will not be enabled until
this TextBox gets keyboard focus
</TextBox>
</DockPanel>
<Window>



9.2 How can I add a CommandBinding to a window using markup?


CommandBinding can be added to a window using markup. Note that in XAML, the CommandBindingCollection is not declared in the markup as an element and the collection object is inferred by the type that the property takes and you populate the property element with one or more CommandBinding elements.

[XAML]

<Window.CommandBindings&rt;
<CommandBinding Command="ApplicationCommands.Open"
Executed="OpenCmdExecuted"
CanExecute="OpenCmdCanExecute"/&rt;
</Window.CommandBindings&rt;



9.3 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");
}
}
}
}
}



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


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<GridViewRowPresenter>(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<T>(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>T<(child);
if (child != null && child.GetType() == typeof(T))
{
break;
}
}
}
return child as T;
}
}


© 2001-2008 Copyright Syncfusion Inc. All rights reserved.  |  Privacy Policy  |  Contact  |  Sitemap