I am building a WPF Custom Control (Note: NOT a user control). The c# code for the control is defined as follows:
using System.Windows;
using System.Windows.Controls;
using MvvmFoundation.Wpf;
namespace TextBoxWithInputBinding
{
public class AutoComp : Control
{
static AutoComp()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(AutoComp), new FrameworkPropertyMetadata(typeof(AutoComp)));
}
public AutoComp()
{
DownCommand = new RelayCommand(() =>
{
System.Diagnostics.Debug.WriteLine("Down");
});
PressedCommand = new RelayCommand(() =>
{
System.Diagnostics.Debug.WriteLine("Pressed");
});
}
public RelayCommand DownCommand
{
get { return (RelayCommand)GetValue(DownCommandProperty); }
set { SetValue(DownCommandProperty, value); }
}
public static readonly DependencyProperty DownCommandProperty =
DependencyProperty.Register("DownCommand", typeof(RelayCommand), typeof(AutoComp), new PropertyMetadata(null));
public RelayCommand PressedCommand
{
get { return (RelayCommand)GetValue(PressedCommandProperty); }
set { SetValue(PressedCommandProperty, value); }
}
public static readonly DependencyProperty PressedCommandProperty =
DependencyProperty.Register("PressedCommand", typeof(RelayCommand), typeof(AutoComp), new PropertyMetadata(null));
}
}
I define the template for the control in Generic.xaml as follows:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TextBoxWithInputBinding">
<Style TargetType="{x:Type local:AutoComp}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:AutoComp}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
>
<StackPanel>
<TextBox>
<TextBox.InputBindings>
<KeyBinding Key="Down" Command="{TemplateBinding DownCommand}"/>
</TextBox.InputBindings>
</TextBox>
<Button Content="Press me" Command="{TemplateBinding PressedCommand}"/>
</StackPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
When I press the "Press me" button the PressedCommand is triggered (the word "Pressed" appears in the output window - however, when I type in the TextBox and press the down key, nothing happens.
What do I need to do to make the DownCommand fire?