读书人

WPF鼠标事件mouseenter鼠标移入改变

发布时间: 2013-01-27 13:56:15 作者: rapoo

WPF鼠标事件mouseenter,鼠标移入改变控件颜色
本帖最后由 qq542369628 于 2013-01-11 14:42:15 编辑 使用WPF新建一个窗口,里面有多个button按钮,我的要求是鼠标移到某个按钮上时,该按钮的背景色改变,移出则恢复原来的颜色,请问代码怎么写,教程里面只写用name该,但是name惟一,每次只能改一个啊,总不能我为所有的按钮都添加mouseenter和mouseleave事件吧?

 private void mouse_Enter1(object sender,RoutedEventArgs e)
{
wrap1.Background = new SolidColorBrush(Colors.Red);
}

private void mouse_Leave1(object sender, RoutedEventArgs e)
{
wrap1.Background = new SolidColorBrush(Colors.AliceBlue);
}

我把每个按钮都绑定上这两个事件(这样做到底可不可以),但是只能改变一个按钮的背景,到底该怎么做呢(我是新手,刚学这个) wpf mouseenter
[解决办法]
<Style TargetType="{x:Type Button}" x:Key="ToolBarBtn">
<Setter Property="Background" Value="#FFE2E2E2"/>
<Setter Property="BorderBrush" Value="#20000000"/>
<Setter Property="MinWidth" Value="90"/>
<Setter Property="Cursor" Value="Hand"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="Back" CornerRadius="3" BorderThickness="1"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}">
<Grid>
<ContentPresenter Margin="2" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>



</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#FFC2E0FF"/>
<Setter Property="BorderBrush" Value="#FF3399FF"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>


把style加到页面的resource里面 所以button都变

读书人网 >C#

热点推荐