Click here to Skip to main content
15,443,778 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have ObservableCollection that is adding elements to textblock in Xaml.
Number of textblock created is same as number of objects in ObservableCollection.

Now, same for textblock, next to each textblock is button. I need to recognize based on property of object(which is in textblock) which button was clicked(cant use onbuttonclick).

What I have tried:

I cant figure out how to do that, because textblock and button arent connected, except in design.

1 solution

You could set the button's Tag property to the object it's associated with.

button.Tag = collection[index];

When the button is clicked, inspect the Tag property to perform some operation on the object.


If it were me, I wouldn't use a series of TextBlock controls with buttons associated with them. I'd use a some sort of collection container, such as a ListBox, ListView, or ComboBox, with a single button that processes the currently selected item in the container.
Share this answer
Member 13568484 20-Feb-18 7:44am     CRLF
First of all, thanks so much for helping me these days :D I dont get it very well, because I have only button defined on template, and I was always using Command={Binding}..and then inside code I would have DelegateCommand which will lead to method on button click. I dont know how I would make It this way now ? Sorry If I didnt understand help
#realJSOP 20-Feb-18 8:32am    
I updated my answer with a design suggestion.

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

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900