【问题标题】:ListBox.SelectedIndex in ContextMenu event handlerContextMenu 事件处理程序中的 ListBox.SelectedIndex
【发布时间】:2013-07-10 17:22:01
【问题描述】:

我有一个带有上下文菜单的列表框。如何在 ContextMenuItem 单击事件处理程序中获取 SelectedIndex (SelectedItem) 属性的值?目前在 Edit_Click 和 Delete_CLick 事件中,CarsList.SelectedIndex 的值始终为 -1。

这是我的 XAML 列表框:

           <ListBox Name="CarsList" Style="{StaticResource ListBoxStyle}" Margin="26,0,26,0" Height="380" >
                <toolkit:ContextMenuService.ContextMenu>
                    <toolkit:ContextMenu Name="ContextMenu" >
                        <toolkit:MenuItem Name="Edit" Header="Edit" Click="Edit_Click"/>
                        <toolkit:MenuItem Name="Delete"  Header="Delete" Click="Delete_Click"/>
                    </toolkit:ContextMenu>
                </toolkit:ContextMenuService.ContextMenu>
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <TextBlock Text="{Binding CarName}" TextTrimming="WordEllipsis" Foreground="Black" FontSize="24" Width="428"/>
                            <TextBlock Text="{Binding VIN}" TextWrapping="Wrap" Foreground="Gray" FontSize="20" Width="428"/>
                            <TextBlock Text="{Binding Date}" TextWrapping="Wrap" Foreground="Gray" FontSize="20" Width="428"/>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

谢谢。

【问题讨论】:

    标签: windows-phone-7 listbox contextmenu


    【解决方案1】:

    首先,您已将Context Menu 分配给ListBox 控件,而不是每个项目。因此,请将 &lt;toolkit:ContextMenuService.ContextMenu&gt; 块移动到 StackPanel

    有几种获取元素的方法,在其上执行上下文菜单单击:

    Click 处理程序中,您有sender 对象(我猜是MenuItem

    将其投射到MenuItem 并查看它的DataContext。它将是您绑定到列表的集合项目。因此,您可以通过以下方式找到索引:

    int selectedIndex = YourListBoxItemCollection.IndexOf((sender as MenuItem).DataContext)
    

    ,其中YourListBoxItemCollection 是您分配给CarsList.ItemsSource 的内容

    【讨论】:

    • 例如,我将 ContextMenu 块移动到 StackPanel 并分配给 ListBox.ItemsSource 值“Cars”。但我在代码中有一个错误 >int selectedIndex = Cars.IndexOf((sender as UIElement).DataContext); //找不到 System.Windows.UIElement 的 DataContext 定义怎么了?
    • 抱歉,DataContextFrameworkElement 中。试试看或MenuItem 而不是UIElement。它应该工作
    • ListBoxItem contextMenuListItem = CarsList.ItemContainerGenerator.ContainerFromItem((sender as ContextMenu).DataContext) as ListBoxItem; //找到了这段代码,但是它抛出了一个异常 NullReferenceException。而且我不明白如何在 ItemTemplate 中读取我的文本框的值?
    • 请逐步调试代码。 var Menu = sender as ContextMenu;Car car = Menu.DataContext as Car;int index = Cars.IndexOf(Car);你哪里有例外?
    • 感谢您的帮助和提示 :) 它现在正在工作(解决方案在下面,在第二个答案下)。
    【解决方案2】:

    看起来您需要将 ListPicker (http://silverlight.codeplex.com/releases/view/75888) 与 SelectedItems 一起使用。 或者 添加一些元素选择的标志...

    【讨论】:

    • 已解决。使用此常见问题解答:windowsphonegeek.com/tips/…
    • 对于未来的问题: >ListBoxItem selectedListBoxItem = CarsList.ItemContainerGenerator.ContainerFromItem((sender as MenuItem).DataContext) as ListBoxItem; int SelectedIndex = CarsList.ItemContainerGenerator.IndexFromContainer(selectedListBoxItem);
    • 这是可行的,但有趣的是,此代码是否有任何优化或更灵活的解决方案?感谢大家的回答。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-25
    • 2023-04-04
    • 1970-01-01
    • 2013-12-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多