简体   繁体   English

确定执行上下文菜单时在列表框中单击了哪个listboxitem

[英]Determining which listboxitem is clicked on in a listbox when executing a context menu

I am trying to use context menu in a listbox to run some come code.that require data from which item it originated.the click event context menu item shows msg but i found that it doent not access the originating listview item . 我正在尝试使用列表框中的上下文菜单来运行一些come.code,这些代码需要它来自哪个项目的数据。单击事件上下文菜单项目显示msg,但我发现它不访问原始listview项目。

<Canvas x:Name="LeftCanvas"  Grid.Column="0" Grid.Row="1" Margin="5,0,0,0">
    <StackPanel>
        <TextBlock Text="Unseated Guests" Background="Blue" Foreground="White" FontFamily="Verdana" FontSize="11" FontWeight="Bold" Height="17" Width="150" HorizontalAlignment="Left" TextAlignment="Center"  Padding="0,4,5,2"></TextBlock>
        <ListBox x:Name="UnseatedPersons" ItemsSource="{Binding}" Height="218"  Width="150" BorderBrush="Blue" BorderThickness="2" HorizontalAlignment="Left" Padding="3,2,2,2" src:FloorPlanClass.DragEnabled="true" MouseEnter="UnseatedPersons_MouseEnter"
             MouseLeave="SourceListBox_MouseLeave">
            <ListBox.ItemTemplate>
                <DataTemplate>
                        <DockPanel>
                            <DockPanel.ContextMenu>
                                <ContextMenu>
                                    <MenuItem Header="Archive Info" Click="bt_click" />
                                    <MenuItem Header="Guest Info" />
                                </ContextMenu>
                            </DockPanel.ContextMenu>
                            <Image Name="imgPerson" Source="{Binding ImagePath}" />
                            <TextBlock Name="txtPersonName" Text="{Binding PersonName}" Padding="2,4,0,0" />
                        </DockPanel>
                    </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </StackPanel>            
</Canvas>

C#: C#:

void bt_click(object sender, RoutedEventArgs e)
{
    MessageBox.Show("my message");
}

Use the sender by casting them to MenuItem . 通过将发送方强制转换为MenuItem来使用发送方。 Like: 喜欢:

void bt_click(object sender, RoutedEventArgs e) 
{ 
    MenuItem originalItem = (MenuItem)sender;
    MessageBox.Show(string.Format("clicked from \"{0}\"", originalItem.Name)); 
}
  1. The sender in the click event will be the MenuItem you clicked. click事件中的发送者将是您单击的MenuItem
  2. Its parent will be the ContextMenu 它的父级将是ContextMenu
  3. The PlacementTarget of the ContextMenu will be the DockPanel . ContextMenuPlacementTarget将是DockPanel
  4. The DockPanel will have the ListBoxItem as an ancestor in the Visual Tree DockPanel将在可视树中将ListBoxItem作为祖先

So to get the ListBoxItem in the click event you can use something similar to this 因此,要在click事件中获取ListBoxItem ,您可以使用与此类似的内容

private void bt_click(object sender, RoutedEventArgs e)
{
    MenuItem clickedMenuItem = sender as MenuItem;
    ContextMenu contextMenu = clickedMenuItem.Parent as ContextMenu;
    DockPanel dockPanel = contextMenu.PlacementTarget as DockPanel;
    ListBoxItem listBoxItem = GetVisualParent<ListBoxItem>(dockPanel);
    MessageBox.Show(listBoxItem.ToString());

    // Update. To display the content of the ListBoxItem
    MessageBox.Show(listBoxItem.Content.ToString());
}

public static T GetVisualParent<T>(object childObject) where T : Visual
{
    DependencyObject child = childObject as DependencyObject;
    // iteratively traverse the visual tree
    while ((child != null) && !(child is T))
    {
        child = VisualTreeHelper.GetParent(child);
    }
    return child as T;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM