【问题标题】:Can I make TextBlock clickable - Based on XML parsing results?我可以使 TextBlock 可点击 - 基于 XML 解析结果吗?
【发布时间】:2011-08-19 22:39:31
【问题描述】:

我有一个包含多个文本块的堆栈面板,这些文本块绑定到 XML 文件的解析结果。

我想让每个结果都有一个 onlick 事件,然后链接到更多信息甚至是 message.show 框,但它必须使用它显示的结果中的值。

这是显示结果的列表框的 .Xaml 部分。)

<ListBox Height="435"
         HorizontalAlignment="Left"
         Name="TradeSearch1"
         VerticalAlignment="Top"
         Width="397"
         Grid.ColumnSpan="3"
         Margin="0,63,0,0">
<ListBox.ItemTemplate>
    <DataTemplate>
        <StackPanel Orientation="Horizontal"
                    Height="200">
            <Image Source="{Binding ImageSource}"
                   Height="150" Width="150"
                   VerticalAlignment="Top"
                   Margin="0,10,8,0"/>
            <StackPanel Width="370" >
                <TextBlock Text="{Binding Title}"
                           TextWrapping="Wrap"
                           Foreground="#FFE51A1A"
                           FontSize="16" />
                <TextBlock Text="{Binding PriceDisplay}" 
                           TextWrapping="Wrap" 
                           FontSize="20" 
                           Foreground="Black" />
                <TextBlock Text="{Binding ListingId}" 
                           TextWrapping="Wrap" 
                           FontSize="20" 
                           Foreground="Black" />
                <TextBlock Text="{Binding Region}" 
                           TextWrapping="Wrap" 
                           FontSize="18" 
                           Foreground="Black" />
            </StackPanel>
        </StackPanel>
    </DataTemplate>
</ListBox.ItemTemplate>

其中一个文本块返回 (ListingID) 的值。理想情况下,当用户单击此结果时,他们将被带到另一个页面,该页面将根据结果的 LIstingID 表单显示更多结果。

列表 ID 为 400332970

有没有办法将该值带到另一个页面并在类似的情况下使用它?其中ListingID.XMl 将是onClick 结果中的listingId。

WebClient Trademe = new WebClient();
Trademe.DownloadStringCompleted += new DownloadStringCompletedEventHandler(Trademe_DownloadStringCompleted);
Trademe.DownloadStringAsync(new Uri("http://api.trademe.co.nz/v1/Listings/" + ListingID.xml));

如果这令人困惑,我很抱歉,我已尽力解释。希望有人知道我在说什么。

【问题讨论】:

    标签: xml silverlight linq windows-phone-7


    【解决方案1】:

    如果我理解正确,您可以使用QueryString 将信息传递到下一页。

    //handle the selectionchanged event (replace 'myObject' with your object type)
    void ListBoxSelectionChanged(object sender, SelectionChangedEventArgs args)
    {
        var lbi = ((sender as ListBox).SelectedItem as myObject);
        if(lbi != null)
        {
            string id = lbi.ListingId.ToString();
            NavigationService.Navigate("/NewPage.xaml?listingid=" + id, UriKind.Relative));    
        }
    }
    

    然后,在您的NewPage.xaml 页面中,您可以读取查询字符串的值。 (您可以添加错误检查,或者改用QueryString.TryGetValue 来避免异常)。

    string id = NavigationContext.QueryString["listingid"];
    

    您可以使用id 作为您发布的下载代码的一部分。

    【讨论】:

    • 不会通过所有的listingID 吗?而不是用户点击的那个?大约有 20 个不同的列表都有自己的列表 ID
    • @Rhys:不,不会。大概您将 ListBox 的 ItemSource 绑定到后面代码中的某个集合。上面答案中提到的myObject 是该集合中的一个对象。因此,与 ListBox 的选定项对应的对象将仅包含被点击的 ListBoxItem 的列表 ID。
    • @keyboardP 好的,听起来对我来说是一个解决方案。我不明白用对象类型替换我的对象?你是说String、Bool等吗?
    • 具有TitlePriceDisplayListingIdRegion 的类的名称是什么?在这种情况下,这将是对象类型,因此将 myObject 替换为该类的名称。
    • 啊,我明白了.. 再次感谢您的帮助。你的传奇!! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多