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.

Community
  • 1
  • 1
Guy Ben-Moshe
  • 874
  • 1
  • 15
  • 23
  • The MotionButton_Click function is inside the binded class, "GenericPanel". – Guy Ben-Moshe Dec 27 '15 at 15:47
  • is it MVVM application? – StepUp Dec 27 '15 at 15:58
  • Yes, it's a MVVM application. – Guy Ben-Moshe Dec 27 '15 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. – Guy Ben-Moshe Dec 27 '15 at 16:09
  • Click event is for when using code behand, Command for when using MVVM. Both are equivalent of each other. If you want to really use the click (I wouldn't see why as you don't use the eventargs) then use a trigger. – Bart Dec 27 '15 at 16:10
  • Still, I don't see how it's a duplicate of the other question, using trigget might be an answer. Not a justified downvote. – Guy Ben-Moshe Dec 27 '15 at 16:13
  • Well so you guys made it a duplicate question even though it's not. Great. – Guy Ben-Moshe Dec 27 '15 at 16:19
  • *Sigh...* The Click event is not a bindable property. Accept that fact, then bind the Button's Command property to a command in your view model. – Clemens Dec 27 '15 at 16:23
  • So the answer to my question is "Forget about click attribute, go for command", as andreask answered me. What it has to do with duplicating is beyond me I guess. – Guy Ben-Moshe Dec 27 '15 at 16:26
  • Quite obviously, the answer to the duplicate question shows you how to bind a Button's Command property to a command in a view model. Exactly what you want to do here. – Clemens Dec 27 '15 at 16:38
  • 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. – Guy Ben-Moshe Dec 27 '15 at 16:51

1 Answers1

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.

andreask
  • 4,248
  • 1
  • 20
  • 25
  • It's not working because the method is not in the window class, it's in the binded object, there is no other way? – Guy Ben-Moshe Dec 27 '15 at 15:57
  • 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? – Guy Ben-Moshe Dec 27 '15 at 16:24
  • The method to be invoked must be declared as `ICommand` instead of an event handler. See https://msdn.microsoft.com/library/ms752308(v=vs.100).aspx or some other commanding tutorial for details about how to declare commands! – andreask Dec 27 '15 at 16:40