繁体   English   中英

事件“点击”后如何获取列表框项的属性

[英]How to get the listbox item's properties after event "tap"

我有一个自定义列表框。 一些集合的项目具有字段“名称”、“文本”、“图像”和“网址”。 其他可能有其他字段(使用模板选择器)。 因此,如果项目具有字段“名称”、“文本”、“网址”和“图像” - 它在列表框中显示为 2 个文本块和 1 个图像。 当我点击图像时 - 程序必须打开新窗口,打开 webBrowser 并转到项目属性“url”中的 URL。 我了解如何将信息从一页传输到另一页,但我不明白如何从项目中获取“url”。 我试过

    private void Video_Tap(object sender, GestureEventArgs e) // event when tap on the image
    {
        New tmp = ((sender as ListBox).SelectedItem as New); // New - is the type of collection's item
        string vid = tmp.Video.url; // Video has fields "image" and "url"

        string destination = string.Format("/Video_Page.xaml?uri={0}", vid );
        NavigationService.Navigate(new Uri(destination, UriKind.Relative));
    }

但发件人有一个图像类型。

您可以调用 Image 发送者的Parent以获取其容器(或多次调用它,具体取决于您的 xaml 的结构方式),然后查看容器的Children以找到您要查找的文本框。 例如,也许您想要做这样的事情(在 xaml 中设置包含您的 url 的文本框的Tag属性)。

var grid = (Grid) ((Image) sender).Parent;
foreach (var child in grid.Children)
{
    if (child is TextBox && ((TextBox) child).Tag == "URL")
    {
        return (Textbox) child;
    }
}

或者,如果您只想要对 ListBox 的始终可用的引用,只需在 xaml 中设置它的x:Name = "_MyListbox" ,它就会成为类的一个字段。

作为最后一个选项,我认为绑定到ListBox.SelectedItem对您来说可能更容易,以便您始终拥有一些包含当前选择的New项目的属性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM