在WP7中从ListBox获取值到EventHandler

时间:2011-09-28 12:25:20

标签: c# xaml windows-phone-7

在我的WP7应用程序中,我有ListBox控件与List<ItemTemplate>集合绑定。在每个ListBoxItem我有Click事件导航到DisplayItem.xaml。每个ItemTemplate对象都有Id属性,必须传递给DispalyItem.xaml。我知道我可以使用EventHandler将此值从点击DisplayItem.xaml传递给QueryString,但如何将其从ListBox项目传递到EventHandler

<ListBox x:Name="listBoxItems">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <Ellipse Fill="red" Width="30" Height="30"></Ellipse>
                        <StackPanel Orientation="Horizontal">
                            <CheckBox IsChecked="{Binding Status}" FontSize="35" />
                        </StackPanel>
                        <StackPanel Orientation="Horizontal">
                            <HyperlinkButton Content="{Binding ItemContent}" Name="itemButton" Click="itemButton_Click"/>
                        </StackPanel>
                        <toolkit:ContextMenuService.ContextMenu>
                            <toolkit:ContextMenu>
                                <toolkit:MenuItem Header="edit"/>
                                <toolkit:MenuItem Header="delete"/>
                            </toolkit:ContextMenu>
                        </toolkit:ContextMenuService.ContextMenu>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
上面的代码中没有提到

Id属性,因为我只是不知道放在哪里。通常,我想知道如何将Id属性恢复为点击EventHandler?我很抱歉,如果这个问题对你来说是基本的,但我是新手,我不知道如何谷歌。

3 个答案:

答案 0 :(得分:1)

如果您是Windows Phone 7的新用户,可能需要停止使用“Click”事件,而是使用ListBox.SelectionChanged事件。如果您被List<MyObject>绑定,则可以执行以下操作:

在您的XAML中:

<ListBox SelectionChanged="NavigateToMyDetail" ... >

然后在后面的代码中,你会有这样的东西:

private void NavigateToMyDetail(object sender, SelectionChangedEventArgs e)
{
   // Make sure that the ListBox change wasn't due to a deselection
   if(e.AddedItems != null && e.AddedItems.Count == 1)
   {
      MyObject selectedItem = (MyObject)e.AddedItems[0];
      // Now you have access to all your MyObject properties
      // and you can pass that to your new page as a parameter
      NavigationService.Navigate(new Uri("DisplayItem.xaml?ItemID=" + selectedItem.id.ToString(), UriKind.Relative));
   }
}

您可以使用以下代码获取该ID(可能在您的“OnNavigatedTo”方法中)。

string myItemID = null;
if(this.NavigationContext.QueryString.ContainsKey("ItemID"))
   myItemID = NavigationContext.QueryString["ItemID"];

希望有所帮助。尝试获取它的另一种方法是给ListBox一个x:Name,然后在Click处理程序中引用它,如:

private void MyClickHandler(object sender, System.Windows.RoutedEventArgs e)
{
   MyObject selectedObject = (MyObject)MyListBoxName.SelectedItem;
}

答案 1 :(得分:1)

如果您将数据绑定与其背后的MVVM视图模型一起使用,则有一个更简单的解决方案。

只需将视图绑定到视图模型中的列表框“Source”的属性,然后对ListBox“SelectedItem”或“SelectedIndex”属性执行相同的操作,然后您将需要的所有内容都可以访问

只考虑意识到(因为我不确定它是否曾被修复)是在选择一个项目时修复所选索引属性,如果你没有将它重置为-1那么如果用户返回到列表中他们无法选择相同的项目。 (在click事件的代码隐藏中执行此操作)

此外,如果您使用MVVM和数据绑定,您可以通过更改Selected项来执行操作,而不是使用Code behind来驱动方向,总是一个选项来保持简单(但不是强制性的)

答案 2 :(得分:0)

我也找到了自己的解决方案。我不确定它的正确位置肯定能解决我现在的问题。 我找到了对象CommandParameter的这个HyperlinkButton属性。我将MyObject.Id属性值绑定到它。

<HyperlinkButton Content="{Binding ItemContent}" Click="itemButton_Click" CommandParameter="{Binding Id}"  /> 

然后在我EventHandler我说:

private void itemButton_Click(object sender, RoutedEventArgs e)
    {
        HyperlinkButton butt = sender as HyperlinkButton;
        NavigationService.Navigate(new Uri("/ViewItem.xaml?itemId=" + butt.CommandParameter.ToString(), UriKind.Relative));
    }

它起作用,因为我需要它才能工作,但我不确定我是否应该在将来的应用程序中使用它。