【问题标题】:WP7 - accessing selected item in listbox when listbox is within an itemtemplateWP7 - 当列表框位于项目模板中时访问列表框中的选定项目
【发布时间】:2011-06-16 06:01:11
【问题描述】:

我有一个包含列表框的数据透视项模板

 <controls:Pivot x:Name="MainPivot" ItemsSource="{Binding PivotItemHeaders}" Title="CLASS TIMETABLE"  >
        <controls:Pivot.HeaderTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Description}"/>
            </DataTemplate>
        </controls:Pivot.HeaderTemplate>
        <controls:Pivot.ItemTemplate>
            <DataTemplate>
                <ListBox x:Name="Events" ItemsSource="{Binding allEventItems}" ItemTemplate="{StaticResource EventDisplay2}"/>
            </DataTemplate>
        </controls:Pivot.ItemTemplate>
    </controls:Pivot>

在后面的代码中,我想访问该列表框的 selectedItem,但我无法“获取”到列表框,因为它(可能)在模板中

this.NavigationService.Navigate(new Uri("/View/EventEdit.xaml?selectedEvent=" +  Events.SelectedItem, UriKind.Relative));

事件列表框未被识别。

假设我可以通过获取对象并将其作为参数传递,我可以使用什么代码来检索它

我知道它的开头是 受保护的覆盖无效 OnNavigatedTo(NavigationEventArgs e) { if (NavigationContext.QueryString.ContainsKey("SelectedEvent")) {

但我不确定从参数中提取对象的语法/代码

了解我如何从这个列表框中获取 selectedItem 以及获取正在传递的对象的代码

  • 谢谢

【问题讨论】:

    标签: windows-phone-7 listbox itemtemplate


    【解决方案1】:

    您可以使用SelectionChanged 事件在值更改时被告知,而不是尝试访问列表框:

    <ListBox x:Name="Events" 
             ItemsSource="{Binding allEventItems}" 
             ItemTemplate="{StaticResource EventDisplay2}"
             SelectionChanged="Event_SelectionChanged" />
    

    然后在你的代码后面:

    private void Event_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        this.selectedEvent = (EventItem)e.AddedItems[0];
    }
    

    您可以使用NavigationContext.QueryString["selectedEvent"] 访问该值,但您只能将字符串存储在导航查询字符串中。如果您的列表框当前绑定到对象,您需要选择一个键,然后使用该键从第二页中查找该事件。

    【讨论】:

    • 所以您建议我捕获已选择的最新项目(通过 SelectionChanged 事件),然后在我想要导航时从该对象中获取密钥并将其传递。跨度>
    • 感谢您的帮助。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多