|
|
9. Concepts Input and Commands.
|
FAQ Home |
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:
|
<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">
|
<Menu DockPanel.Dock="Top">
|
<MenuItem Command="ApplicationCommands.Paste" Width="75" />
|
<TextBox BorderBrush="Black" BorderThickness="2" Margin="25"
|
The MenuItem will not be enabled until
|
this TextBox gets keyboard focus
|
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.
|
<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:
|
using System.Windows.Controls;
|
using swf = System.Windows.Forms;
|
using System.Windows.Interop;
|
using System.Windows.Forms.Integration;
|
using System.Windows.Input;
|
public partial class CustomShortcutHandlingDemo : Window
|
public CustomShortcutHandlingDemo()
|
// 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?
|
 |
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;
|
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;
|
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))
|
child = GetFrameworkElementByName>T<(child);
|
if (child != null && child.GetType() == typeof(T))
|
|
|
|
|