【问题标题】:Getting value back from ListBox to EventHandler in WP7在 WP7 中将值从 ListBox 恢复到 EventHandler
【发布时间】:2011-09-30 20:52:28
【问题描述】:

在我的 WP7 应用程序中,我有与 List<ItemTemplate> 集合绑定的 ListBox 控件。在每个ListBoxItem 上,我都有Click 事件导航到DisplayItem.xaml。每个ItemTemplate 对象都有Id 属性,该属性必须传递给DispalyItem.xaml。我知道我可以使用 QueryString 将这个值从 Click EventHandler 传递给 DisplayItem.xaml 但如何将它从 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 ?很抱歉,如果这个问题对你来说是基本的,但我是新手,我不知道如何用谷歌搜索。

【问题讨论】:

    标签: c# xaml windows-phone-7


    【解决方案1】:

    如果您真的是 Windows Phone 7 的新手,您可能希望停止使用“Click”事件,而使用 ListBox.SelectionChanged 事件。如果您绑定到List&lt;MyObject&gt;,您可以执行以下操作:

    在您的 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;
    }
    

    【讨论】:

      【解决方案2】:

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

      只需将视图绑定到视图模型中列表框“Source”的属性,然后对列表框“SelectedItem”或“SelectedIndex”属性执行相同操作,即可在任何需要的地方访问所需的一切.

      只想知道(因为我不确定它是否得到修复)是在选择项目时修复选定的索引属性,如果您不将其重置为 -1,那么如果用户返回他们无法选择相同项目的列表。 (在点击事件的代码隐藏中执行此操作)

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

      【讨论】:

        【解决方案3】:

        我也找到了自己的解决方案。我不确定它是否正确,它现在肯定能解决我的问题。 我发现对象HyperlinkButton 的这个CommandParameter 属性。我将我的 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));
            }
        

        它可以根据我的需要工作,但我不确定将来是否应该在我的应用程序中使用它。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-04-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-08-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多