Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The simple way to do this would be data binding. In WPF this is quite easy. You can bind the data from one control to another. Below I have bound a TextBox, which has taken the user input, to a Label which displays it.</p> <pre><code>&lt;Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"&gt; &lt;Grid&gt; &lt;TextBox Height="23" HorizontalAlignment="Left" Margin="45,55,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" /&gt; &lt;Label Content="{Binding ElementName=textBox1, Path=Text}" Height="28" HorizontalAlignment="Left" Margin="45,135,0,0" Name="label1" VerticalAlignment="Top" Width="142" /&gt; &lt;/Grid&gt; </code></pre> <p></p> <p>The more complex answer would be to do Command binding. What this does is catches specific key bindings on a UserControl or Window and executes a given command. This is a little more complex because it requires you to create a class that implements ICommand.</p> <p>More info here: <a href="http://msdn.microsoft.com/en-us/library/system.windows.input.icommand.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/system.windows.input.icommand.aspx</a></p> <p>I personally am a big fan of the RelayCommand implementation by Josh Smith. There is a great article <a href="http://msdn.microsoft.com/en-us/magazine/dd419663.aspx#id0090051" rel="nofollow">here</a>.</p> <p>In short though you can do this.</p> <p><strong>XAML:</strong></p> <pre><code>&lt;Window.InputBindings&gt; &lt;KeyBinding Key="Escape" Command="{Binding KeyPressCommand}"/&gt; &lt;/Window.InputBindings&gt; </code></pre> <p><strong>CODE:</strong> You will need to create a RelayCommand Class.</p> <pre><code>public class RelayCommand : ICommand { private readonly Action&lt;object&gt; _execute; private readonly Predicate&lt;object&gt; _canExecute; public RelayCommand(Action&lt;object&gt; execute, Predicate&lt;object&gt; canExecute) { if (execute == null) throw new ArgumentNullException("execute"); _execute = execute; _canExecute = canExecute; } public event EventHandler CanExecuteChanged { add { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested -= value; } } public void Execute(object parameter) { _execute(parameter); } public bool CanExecute(object parameter) { return _canExecute == null || _canExecute(parameter); } </code></pre> <p>And then to implement this in your main window you will need to do this.</p> <pre><code> private RelayCommand _keyPressCommand; public RelayCommand KeyPressCommand { get { if (_keyPressCommand== null) { _keyPressCommand = new RelayCommand( KeyPressExecute, CanKeyPress); } return _keyPressCommand; } } private void KeyPressExecute(object p) { // HANDLE YOUR KEYPRESS HERE } private bool CanSaveZone(object parameter) { return true; } </code></pre> <p>If you go with the second option I really suggest you take a look at the MSDN article by Josh Smith.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload