wpf 中button那上面没有鼠标没有按键的感觉
时间: 2023-12-09 21:04:38 浏览: 31
在 WPF 中,Button 控件默认情况下是没有鼠标和按键的感觉的,但是你可以通过修改样式来实现在 Button 上显示鼠标和按键的感觉。
以下是一种常见的实现方式:
1. 在 WPF 应用程序中打开 MainWindow.xaml 文件。
2. 在 Window 标签中添加以下属性:
xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
3. 在 Window 中添加以下样式:
<Window.Resources>
<Style x:Key="ButtonStyle" TargetType="{x:Type Button}">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid>
<Microsoft_Windows_Themes:ButtonChrome x:Name="Chrome" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderPressed="{TemplateBinding IsPressed}" SnapsToDevicePixels="true">
<Grid>
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Content="{TemplateBinding Content}" RecognizesAccessKey="True"/>
</Grid>
</Microsoft_Windows_Themes:ButtonChrome>
<Rectangle x:Name="ButtonOverlay" Opacity="0" Fill="#FF000000"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="ButtonOverlay" Property="Opacity" Value="0.2"/>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="ButtonOverlay" Property="Opacity" Value="0.4"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
4. 在 Button 控件中应用样式:
<Button Style="{StaticResource ButtonStyle}" Content="Button"/>
这样就可以在 Button 控件上显示鼠标和按键的感觉了。需要注意的是,这种方式需要使用 PresentationFramework.Aero 库,如果你的应用程序中没有引用该库,需要先添加该库的引用。