WPF 右键菜单样式
时间: 2024-02-16 07:57:24 浏览: 130
以下是一个WPF右键菜单的样式示例:
```xaml
<Window x:Class="ContextMenuStyleExample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<ContextMenu x:Key="MyContextMenu">
<MenuItem Header="Cut"/>
<MenuItem Header="Copy"/>
<MenuItem Header="Paste"/>
</ContextMenu>
<Style TargetType="{x:Type Button}">
<Setter Property="ContextMenu" Value="{StaticResource MyContextMenu}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<ContentPresenter HorizontalAlignment="Center"
VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<Button Content="Right-click me" Width="100" Height="30"/>
</Grid>
</Window>
```
在这个示例中,我们定义了一个名为“MyContextMenu”的ContextMenu,其中包含三个MenuItem。然后,我们定义了一个Button样式,将ContextMenu设置为MyContextMenu。最后,我们在Grid中放置了一个Button,它将使用我们定义的样式。
阅读全文