markls Asked:2022-07-13 17:17:41 +0000 UTC2022-07-13 17:17:41 +0000 UTC 2022-07-13 17:17:41 +0000 UTC 为项目中的所有类对象创建一个事件 772 有一个项目WPF,它有几个表单和很多按钮。有没有办法创建所有按钮событие?就我而言MouseEnter。当用光标悬停在按钮上时,我需要按钮来改变颜色。 我不想为每个人手动签署活动...... c# wpf 1 个回答 Voted Best Answer aepot 2022-07-13T20:12:32Z2022-07-13T20:12:32Z 要更改按钮的颜色,不需要处理程序,有样式和触发器。 标准按钮模板有点复杂,所以我没有深入研究,自己写了。特别是,在标准模板上,在悬停时更改按钮的颜色并不容易。 <Window x:Class="WpfApp1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApp1" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Window.Resources> <Style TargetType="Button"> <Setter Property="OverridesDefaultStyle" Value="True"/> <Setter Property="SnapsToDevicePixels" Value="True"/> <Setter Property="Margin" Value="5"/> <Setter Property="Background" Value="LightGreen"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Button"> <Border BorderThickness="1" BorderBrush="Gray" Background="{TemplateBinding Background}" CornerRadius="4"> <ContentPresenter HorizontalAlignment="Center" Margin="2"/> </Border> </ControlTemplate> </Setter.Value> </Setter> <Style.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Background" Value="LightPink"/> </Trigger> <Trigger Property="IsMouseDirectlyOver" Value="True"> <Setter Property="Background" Value="Red"/> </Trigger> </Style.Triggers> </Style> </Window.Resources> <Grid> <StackPanel> <Button Content="Button 1"/> <Button Content="Button 2"/> </StackPanel> </Grid> </Window> 它看起来像这样。 鼠标悬停在这里Button 2。 0 行 C# 代码,一切都适用于任意数量的按钮。
要更改按钮的颜色,不需要处理程序,有样式和触发器。
标准按钮模板有点复杂,所以我没有深入研究,自己写了。特别是,在标准模板上,在悬停时更改按钮的颜色并不容易。
它看起来像这样。
鼠标悬停在这里
Button 2。0 行 C# 代码,一切都适用于任意数量的按钮。