views:

95

answers:

1

I have a VB.net WPF application that creates a list from a databind. For each row in the databind, it generates a textblock that displays the information from different fields in the databind. It also generates a button which I would like to run a subroutine when clicked.

When I click the button, I want it to be able to get the information from the databind on the column the button is clicked, and to pass it to the subroutine so the code-behind can use that information.

Any ideas?

+1  A: 

This sounds like a job for a Routed Command. Pass your data object as the command parameter, and set up a command binding to handle when the command is invoked. So, in your data template, you'd have something like:

<Button Command="YourCommandHere" CommandParameter={TemplateBinding SomeProperty}" />

Then, in the handler for the command (set up through a CommandBinding) you'll have the command parameter passed to you in the Parameter property of the ExecutedRoutedEventArgs. Read the article that I linked to.

FMM
Do you have an example of some code using the CommandParameter from a button control. I can currently set up a new RoutedCommand with a button, but I am unable to retreive the parameter in the code behind.
Ian
Ian: See edits.
FMM
Thanks a bundle, I redid the example from MSDN after reading your answer and managed to implement it in my app.
Ian