0

I'm trying to bind a function with a "click" attribute of a Button,

A CLICK ATTRIBUTE, NOT A COMMAND. THANK YOU.

here is what I tried to do:

The Xaml Code line:

<Button Background="{Binding Path=motionColor}" Click="{Binding MotionButton_Click}" />

The c# relevant code: (which inside the binded object, and not the window class)

public void MotionButton_Click(object Sender, RoutedEventArgs e)
{
        SendPacket(cockpitType, (byte)Index.Motion, MotionValue);
        setMotion(3);       
}

Some notes:

  • The background binding works just fine.
  • I have tried to do it as "Path=MotionButton_Click", not working either.
  • Here is the error I get:

Error 1 Click="{Binding MotionButton_Click}" is not valid. '{Binding MotionButton_Click}' is not a valid event handler method name. Only instance methods on the generated or code-behind class are valid. Line 50 Position 94. c:\users\dp27317\documents\visual studio 2010\Projects\GenericSoundMotion\GenericSoundMotion\MainWindow.xaml 50 94 GenericSoundMotion

  • Not sure if important but the binded collection is "ObservableCollection" of "public class GenericPanel".

All I want is that when I press on that button, the "MotionButton_Click" function will run. Everything that will make it work is a blessing, even dirty solutions.

Thank you.

11
  • The MotionButton_Click function is inside the binded class, "GenericPanel". Commented Dec 27, 2015 at 15:47
  • is it MVVM application? Commented Dec 27, 2015 at 15:58
  • Yes, it's a MVVM application. Commented Dec 27, 2015 at 16:00
  • Sigh... It's not a command, it's a click... Believe me, I search real deep before asking a question. I would appreciate if you cancel that downvote. Commented Dec 27, 2015 at 16:09
  • 1
    Since the answers are the same, the question is a duplicate, I understand now, There are no different questions with the same answer, make sense. Thanks for the help. Commented Dec 27, 2015 at 16:51

1 Answer 1

2

On a WPF button, Click is an event, so you can't bind anything to it. You can either take a look at commanding (since binding to the button's Command property will have the same effect as specifying a Click event handler: a method that is invoked when clicking the button), or move the event handler to the window class, and call the method in the binded class from there, e.g. by referncing the window's DataContext.

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

3 Comments

It's not working because the method is not in the window class, it's in the binded object, there is no other way?
So I've tried to make it happen with the "Command="{Binding Path=MotionButton_Click}"" attribute, the function doesn't run on click but there are no errors, any idea?
The method to be invoked must be declared as ICommand instead of an event handler. See msdn.microsoft.com/library/ms752308(v=vs.100).aspx or some other commanding tutorial for details about how to declare commands!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.