Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: XAML WPF
I'm trying to write an click event for save button. However, when I write in xaml, there is no OnClick function or Click function for me.
So, how can write an event for the button to perform some action?
Posted 17-Jun-13 22:55pm
Pheonyx at 18-Jun-13 3:58am
Where is the XAML for your button declaration?
sandra_tay at 18-Jun-13 4:02am
<Button Content="Save" Height="27" Width="50"/>
this code is inside a user control that I created.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

<button content="Save" height="27" width="50" click="Button_Click" />
In XAML the event is "Click" not "OnClick" as it was in windows forms.
sandra_tay at 18-Jun-13 4:08am
I have try to using "Click" but it does not having this event. And it will only show me "ClickMode"
Pheonyx at 18-Jun-13 4:12am
It is definitely called Click. Where about's are you trying to define this? In the button definition within the user control, or in the main window that is hosting the user control?
sandra_tay at 18-Jun-13 4:17am
Yes, I define this within the user control.
Pheonyx at 18-Jun-13 4:21am
Can you show the complete XAML for the user control please?
sandra_tay at 18-Jun-13 4:21am
I have try again, its work, thank you so much
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

You should learn MVVM. It would seem that save should be part of the ViewModel, not part of the View. In this case you would bind a Command to a property in the ViewModel with a type of ICommand. I have done some projects using WPF, but not MVVM, and have found that it is a mistake. Much easier to maintain the MVVM pattern than to deal with directly handling events.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Maciej Los 495
1 Sergey Alexandrovich Kryukov 479
2 OriginalGriff 360
3 CHill60 280
4 CPallini 250
0 Sergey Alexandrovich Kryukov 10,102
1 OriginalGriff 9,495
2 Peter Leow 5,241
3 Kornfeld Eliyahu Peter 3,373
4 Maciej Los 3,076

Advertise | Privacy | Mobile
Web04 | 2.8.150327.1 | Last Updated 18 Jun 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100