开发者

Context menu event of listbox in wpf

I am creating static context menu in list box. The context menu is showed, but its click event does not work.

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

How can I fix this?


I'm not completely sure why, but setting the context menu for the whole DockPanel seems to work:

<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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜