0

I'm having some difficulty finding simple examples while using WPF when it comes to control bindings and I'm hoping you can help me get my head around it with this simple example.

Can you please explain why this doesn't work and also a simple way to get it running?

I've looked at numerous tutorials but they are all still a little advanced for me at this stage I think so any help is greatly appreciated.

Thanks!

XAML:

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="DemoProject.MainWindow"
    xmlns:custom="clr-namespace:DemoProject"
    Title="DemoProject" >

    <TextBox x:Name="MyTextBox">
        <TextBox.InputBindings>
            <KeyBinding Key="Enter" Command="{x:Static custom:MainWindow.CommandEnterKeyPressed}" 
                CommandParameter="{Binding Path=Text, RelativeSource={RelativeSource AncestorType={x:Type TextBox}}}" />
        </TextBox.InputBindings>
    </TextBox>

    <Window.CommandBindings>
        <CommandBinding Command="{x:Static custom:MainWindow.CommandEnterKeyPressed}"
            Executed="CommandEnterKeyPressedExecuted" />
    </Window.CommandBindings>   
</Window>

C#:

namespace DemoProject
{
    public partial class MainWindow : Window
    { 
        private static RoutedUICommand CommandEnterKeyPressed;

        public MainWindow()
        {
            InitializeComponent();
        }

        public static RoutedUICommand CommandEnterKeyPressed = new RoutedUICommand();

        private void CommandEnterKeyPressedExecuted(object sender, CanExecuteRoutedEventArgs e)
        {
            MessageBox.Show("Enter key was pressed");
        }
    }
}

When I run this, I get the errors

"The member "CommandEnterKeyPressed" is not recognised or is not accessible" and "No overload for "'ommandEnterKeyPressed' matches delegate 'System.Windows.Input.ExecutedRoutedEventHandler'".

Is there something simple I'm missing?

Thanks.

1 Answer 1

1

Change CanExecuteRoutedEventArgs to ExecutedRoutedEventArgs

private void CommandEnterKeyPressedExecuted(object sender, CanExecuteRoutedEventArgs e)

Should be

private void CommandEnterKeyPressedExecuted(object sender, ExecutedRoutedEventArgs e)

CanExecuteRoutedEventArgs is used for CanExecute event. You should also remove this line

private static RoutedUICommand CommandEnterKeyPressed;

and leave only public declaration of your RoutedUICommand

Sign up to request clarification or add additional context in comments.

3 Comments

Wow - can't believe I didn't spot that! Thanks! That's helped get me on the right track. I've now got a problem with the <CommandBinding> tag in XML "Value cannot be null. Parameter name: value" - do you know what could be causing that?
So CommandBinding has now Command and Executed event handler? Is the problem in design?
Thanks - yes, I had initialized the component before creating the RoutedUICommand. Works perfectly now, thank you for your help :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.