0

I have a button in WPF window. I want to display a message by both pressing left mouseclick and by clicking Ctrl+F. I want most of the code in XAML. Code is as given below. My problem is that mouse click is working for me but not the key press. Can anybody please help me. Thanks in advance.

MyWindow.xaml

<Window x:Class="Commands_Xaml.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">
    <Grid>
        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="164,88,0,0" Name="button1" VerticalAlignment="Top" Width="75">
            <Button.CommandBindings>
                <CommandBinding Command="ApplicationCommands.Find" Executed="CommandBinding_Executed"/> 
            </Button.CommandBindings>
            <Button.InputBindings>
                <KeyBinding Key="F" Modifiers="Control" Command="ApplicationCommands.Find"/>
                <MouseBinding MouseAction="LeftClick" Command="ApplicationCommands.Find"/>
            </Button.InputBindings>
        </Button>
    </Grid>
</Window>


MainWindow.xaml.cs

namespace Commands_Xaml
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();            
            //ApplicationCommands.Find.InputGestures.Add(new KeyGesture(Key.F,ModifierKeys.Control));
            //ApplicationCommands.Find.InputGestures.Add(new MouseGesture(MouseAction.LeftClick));

            //CommandBinding bindingObject = new CommandBinding();
            //bindingObject.Command = ApplicationCommands.Find;
            //bindingObject.Executed += new ExecutedRoutedEventHandler(CommandBinding_Executed);
            //this.CommandBindings.Add(bindingObject);
        }

        private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            MessageBox.Show("Button clicked");
        }
    }
}
2
  • Are you using the correct Mouse Command event method for your command? Commented Dec 4, 2012 at 19:21
  • Yes Bob, just for understanding purpose I wrote my code. It is working for Mouse click but not for Key input. Commented Dec 5, 2012 at 3:36

1 Answer 1

3

Move the command binding / input binding to the window level and use the Command property of the button instead of trying to use a MouseBinding.

<Window x:Class="WpfApplication2.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">

    <Window.CommandBindings>
        <CommandBinding Command="ApplicationCommands.Find" Executed="CommandBinding_Executed" />
    </Window.CommandBindings>
    <Window.InputBindings>
        <KeyBinding Command="ApplicationCommands.Find" Key="F" Modifiers="Control" />
    </Window.InputBindings>

    <Grid>
        <Button Command="ApplicationCommands.Find" Content="Button" HorizontalAlignment="Left" Margin="206,152,0,0" VerticalAlignment="Top" Width="75"/>
    </Grid>
</Window>

UPDATE

To have different behavior based on different button clicks, this can be accomplished a few ways. Here are two:

Use a different command for each button:

<Button Command="ApplicationCommands.Find" Content="Button1" />
<Button Command="ApplicationCommands.Print" Content="Button2" />

Use CommandParameter:

<Button Command="ApplicationCommands.Find" CommandParameter="Find1" Content="Button1" />
<Button Command="ApplicationCommands.Find" CommandParameter="Find2" Content="Button2" />

And for the KeyBinding:

<KeyBinding Command="ApplicationCommands.Find" CommandParameter="Find3" />

You can access this property in the CommandBinding.Executed handler:

private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
    if (e.Parameter as string == "Find1")
    {
        //Find1
    }
    else if (e.Parameter as string == "Find2")
    {
        //Find2
    }    
    else if (e.Parameter as string == "Find3")
    {
        //Find3
    }
}
Sign up to request clarification or add additional context in comments.

10 Comments

Thanks RQDQ, your suggestions worked for me.But I have few queries:1.For Mouse click, don't we need to do MouseBinding.If so, then why. Because my mouse click is also working without doing any thing specific for it? 2.Suppose I want to have one more button and I want a common event handler for these 2 buttons.Inside this event handler I want to perform some tasks depending on the different button clicks.How to do that?Reason of asking this question is that when I saw the sender in CommandBinding_Executed(),I found it as Window,and I want to get button info here instead of window info.
@WpfBee - there are a few ways to have different behavior based on different command invocations. I have added two.
@RQDQ- my query #1, can you please answer it. query#2, I want to receive the button object as sender inside the command handler which I am still not getting. Receiving command parameter (Find1/Find2) works when mouse click is used but if Qtrl+F is tried then command parameter received inside command handler is null.
@WpfBee - you can set a CommandParameter on the KeyBinding as well. You won't receive the button object as sender when the KeyBinding is used as the button isn't involved.
@RQDQ- OK, so there is no way to receive button as object with KeyBinding? But why we don't need MouseBinding in your example and still mouse click is working?
|

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.