Note that there are some explanatory texts on larger screens.

plurals
  1. POBind to a UserControl RoutedCommand from parent window
    primarykey
    data
    text
    <p>I'm trying to create a UserControl that displays a multi-page image and allows the user to zoom, rotate, and browse an image. The one part I'm having a problem with is getting the keyboard shortcuts setup properly. I figured out I need to have the InputBindings setup on the Window class that is hosting the UserControl. I did figure out how to create an InputBinding in the code behind but I'm expecting a lot of keyboard shortcuts and I think having them in the XAML will be easier. Here is an example project that I'm testing this out in:</p> <p>MainWindow.xaml</p> <pre><code>&lt;Window x:Class="CommandTest.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:CommandTest" Title="Window1" Height="300" Width="344"&gt; &lt;Window.InputBindings&gt; &lt;KeyBinding Key="N" CommandTarget="x:UCon" /&gt; &lt;/Window.InputBindings&gt; &lt;StackPanel&gt; &lt;local:NestedControl x:Name="UCon"&gt; &lt;/local:NestedControl&gt; &lt;/StackPanel&gt; &lt;/Window&gt; </code></pre> <p>MainWindow.Xaml.cs</p> <pre><code>using System.Windows; using System.Windows.Input; namespace CommandTest { public partial class Window1 : Window { public Window1() { InitializeComponent(); KeyGesture keyg = new KeyGesture(Key.OemPlus, ModifierKeys.Control); KeyBinding kb = new KeyBinding((ICommand)UCon.Resources["Commands.ZoomOut"], keyg); kb.CommandTarget = UCon; this.InputBindings.Add(kb); } } } </code></pre> <p>UserControl1.xaml</p> <pre><code>&lt;UserControl x:Class="CommandTest.NestedControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt; &lt;UserControl.Resources&gt; &lt;!-- UI commands. --&gt; &lt;RoutedCommand x:Key="Commands.ZoomOut" /&gt; &lt;/UserControl.Resources&gt; &lt;UserControl.CommandBindings&gt; &lt;CommandBinding x:Name="ZoomCommand" Command="{StaticResource Commands.ZoomOut}" CanExecute="ZoomCommand_CanExecute" Executed="ZoomCommand_Executed"/&gt; &lt;/UserControl.CommandBindings&gt; &lt;Button Command="{StaticResource Commands.ZoomOut}"&gt;Zoom&lt;/Button&gt; &lt;/UserControl&gt; </code></pre> <p>UserControl1.xaml.cs</p> <pre><code>using System.Windows.Controls; using System.Windows.Input; namespace CommandTest { public partial class NestedControl : UserControl { public NestedControl() { InitializeComponent(); } private void ZoomCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e) { e.CanExecute = true; } private void ZoomCommand_Executed(object sender, ExecutedRoutedEventArgs e) { System.Windows.MessageBox.Show("Zoom", "Zoom"); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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