我在列表框中创建静态上下文菜单。显示上下文菜单,但其单击事件不起作用。
<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.Resources>
<ContextMenu x:Key="ctxtmenu">
<MenuItem Header="Archive Info" Click="bt_click"></MenuItem>
<MenuItem Header="Guest Info"></MenuItem>
</ContextMenu>
</DockPanel.Resources>
<Image Name="imgPerson" Source="{Binding ImagePath}" ContextMenu="{StaticResource ctxtmenu}"></Image>
<TextBlock Name="txtPersonName" Text="{Binding PersonName}" Padding="2,4,0,0" ContextMenu="{StaticResource ctxtmenu}"></TextBlock>
</DockPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</Canvas>
我该如何解决这个问题?
答案 0 :(得分:1)
我不完全确定原因,但为整个DockPanel
设置上下文菜单似乎有效:
<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>