从动态按钮获取属性.请帮忙! [英] Getting properties from a dynamic button. Please Help!
问题描述
我正在编写一个触摸屏自动点唱机应用程序.
我是WPF的新手,并热爱它.
一个问题.我创建了一个玻璃效果按钮,以编程方式显示3个数据.即,在选择歌手之后,一个列表框会充满玻璃按钮,其中包含有关歌曲的信息.艺术家名称,曲目名称和(我需要的)trackID号.
我在创建所有按钮时都为其分配了相同的单击事件,因此在单击事件上,如何从按下的按钮中检索trackid ??????
这是我必须处理click事件的代码
私人void SongClick(object sender,RoutedEventArgs e)
{
按钮b =(按钮)发送器;
//需要访问按钮中包含的最后一个文本块中的trackid
}
视觉树是
按钮
-边框
----网格
------ TextBlock1-艺术家
------ TextBlock2-SongName
------ TextBlock3-TrackID(这是我需要的数据)
提前致谢.这真的使我一时陷入困境.
I am writing a touchscreen jukebox application.
I am new to WPF and loving it.
One Problem. I have created a glass effect button to display 3 pieces of data programatically. i.e. on selection of an artist a listbox is filled with glass buttons containing information about the song. Artist name, track name and (the one I need) trackID number.
I have assigned all buttons the same click event when they are created so on the click event how can i retrieve the trackid from the button that is pressed??????
This is the code behind i have to handle the click event
private void SongClick(object sender, RoutedEventArgs e)
{
Button b = (Button)sender;
//need to access the trackid which is in the last textblock contained within the button
}
The visual tree is
Button
--Border
----Grid
------TextBlock1 - Artist
------TextBlock2 - SongName
------TextBlock3 - TrackID (This is the data I need)
Thanks in advance. This is really holding me up at the minute.
推荐答案
您可以看看
谢谢.
WPF的概念仍是新手.
我将代码更改为此-
Thanks.
Still new to the concepts of WPF.
I changed my code behind to this -
private void SongClick(object sender, RoutedEventArgs e) { Button b = (Button)sender; Border brd = (Border)VisualTreeHelper.GetChild(b, 0); Grid grd = (Grid)VisualTreeHelper.GetChild(brd, 0); TextBlock tb = (TextBlock)VisualTreeHelper.GetChild(grd, 2); MessageBox.Show("TrackID is " + tb.Text); }
上面的代码访问了此可视化树-
按钮
-边框
----网格
------ TextBlock1-艺术家
------ TextBlock2-SongName
------ TextBlock3-TrackID(这是我需要的数据)
感谢您的宝贵时间.
The code above accessed this visual tree -
Button
--Border
----Grid
------TextBlock1 - Artist
------TextBlock2 - SongName
------TextBlock3 - TrackID (This is the data I needed)
Thanks for your time Abhinav.
这篇关于从动态按钮获取属性.请帮忙!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!