根据文本块XAML中的属性单击哪个按钮 [英] Which button is clicked based on property in textblock XAML

查看:65
本文介绍了根据文本块XAML中的属性单击哪个按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有ObservableCollection,它将元素添加到Xaml中的textblock。

创建的文本块数与ObservableCollection中的对象数相同。



现在,对于textblock来说,每个文本块旁边都是按钮。我需要根据对象的属性(在文本块中)识别哪个按钮被点击(不能使用按钮点击)。



我尝试了什么:



我无法弄清楚如何做到这一点,因为文本块和按钮没有连接,除了设计。

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.

推荐答案

您可以将按钮的Tag属性设置为与其关联的对象。



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

button.Tag = collection[index];





单击按钮时,检查标记属性对对象执行一些操作。



编辑========================== =======



如果是我,我不会使用一系列 TextBlock 带有与之关联的按钮的控件。我会使用某种集合容器,例如 ListBox ListView ComboBox ,只有一个按钮,用于处理容器中当前选定的项目。



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

EDIT=================================

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.


这篇关于根据文本块XAML中的属性单击哪个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆