2011-07-27 21 views
0

我想在列表框中使用上下文菜單來運行一些來源code.that,它需要來自哪個項目的數據。click事件上下文菜單項顯示msg但我發現它不會訪問始發列表視圖項目。確定在執行上下文菜單時在列表框中單擊了哪個listboxitem

<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#:

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

東西,你問9個問題,這樣的單擊事件遠不接受單一答案。請接受一些答案。你的代碼也不完整。 –

回答

2

他們鑄造MenuItem使用發件人。像:

void bt_click(object sender, RoutedEventArgs e) 
{ 
    MenuItem originalItem = (MenuItem)sender; 
    MessageBox.Show(string.Format("clicked from \"{0}\"", originalItem.Name)); 
} 
+0

我想知道你點擊了哪個列表框 – user644194

+0

@user:如果它們不是'MenuItem',那麼轉換爲'ListBoxItem'代替So:'ListBoxItem originalItem =(ListBoxItem)sender;'這裏你得到了點擊的列表框項。 –

1
  1. click事件中的發件人將是您單擊的MenuItem
  2. 其母公司將是ContextMenu
  3. ContextMenuPlacementTarget將是DockPanel
  4. DockPanel將有ListBoxItem作爲視覺樹

所以祖先得到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; 
} 
+0

它只顯示system.data.datarowview – user644194

+0

@ user644194:MessageBox只是一個例子,如果你在那裏得到datarowview,那麼你似乎成功地得到了ListBoxItem,或者我誤解了一些東西? –

+0

@ user644194:更新了我的答案,這是你在找什麼? –

相關問題