在WPF里彈出菜單是用Popup,你那個右鍵的是上下文菜單(也就是快捷菜單)。
<Grid> <Button x:Name="BtnPop" Width="100" Height="30" HorizontalAlignment="Left" VerticalAlignment="Bottom" Content="Popup" Click="BtnPop_Click"/> <Popup x:Name="Pop" PopupAnimation="Slide" Width="100" Height="100" PlacementTarget="{Binding ElementName=BtnPop}" Placement="Top" AllowsTransparency="True" StaysOpen="False"> <Border Background="#FFCFCFCF" CornerRadius="5" BorderBrush="#FF000000" BorderThickness="1"> <StackPanel Margin="5"> <Button Content="A" Margin="5"/> <Button Content="B" Margin="5"/> <Button Content="C" Margin="5"/> </StackPanel> </Border> </Popup> </Grid>
BtnPop按鈕的Click事件為:Pop.IsOpen = True。
補充一點:
可以將BtnPop的LostFocus事件設置為:Pop.IsOpen = False,以此來關閉Popup。
再補充一點:
如果你希望Popup中的按鈕像ContextMenu中的那樣,也就是不要按鈕效果,可以自定義一個Button的Style。然后把Popup中的按鈕的Style都換成自定義的這個Style。