Buttons in a view triggering a method in code behind - clicked vs command - SIMPLE info for new people! #21538
Unanswered
antonycartwright
asked this question in
Show and tell
Replies: 1 comment
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi everyone,
I just thought I'd post this for anyone else who is about to struggle!
Sometimes, you need a really simple and quick fix to something rather than going around the houses for hours!
In my case, I have a collectionview with a grid. The collectionview is bound by the code behind rather than in the XAML (I prefer this approach because I think it's more flexible). Each item in the template has a button that I added, and the idea of the button is that it determines the item's bound object in the item/row, and then navigates to an edit page.
I'd done research and ICommand was suggested, mostly for the ability to pass a parameter. Basically you add a command, you add the command attribute for the control, and the command parameter. You think it's going to be easy like Command="YourMethod" (referring to a method in the code behind. It doesn't work like that.
You have to mess around with awful text like Command = "{Binding Command="", Path=""}", blah blah blah... It wants you to bind to a VM... It's confusing in general with finicky syntax. You don't have to do all that crap! Don't waste hours like I did with this ridiculous problem.
The solution is very simple! Just use the easy "clicked" event. Sure, you can;t pass a parameter, but you can attain it from the underlying item as shown below.
<Button Grid.Column="3" Text="Edit" Clicked="Button_Clicked"/>
The code behind is extremely simple:
Don't waste ages like I did... Don't bother with that Command thing - it's overly complicated!
Antony
Beta Was this translation helpful? Give feedback.
All reactions