如何拦截鼠标上的按钮



我在C#中有一个WPF应用程序。当我运行我的应用程序时,它会动态地创建一个带有背景图像的按钮。

这是示例代码:

Button button = new Button();
button.Width = 130;
button.Height = 190;
ImageBrush brush = new ImageBrush(new BitmapImage(new Uri(Directory.GetCurrentDirectory()+@"Imagesnao_grigio.png")));
brush.Stretch = Stretch.Uniform;
buttonCoppia.Background = brush;
button.Background = brush;

它是有效的,所以当我用鼠标传递按钮时,图像会更改为默认的按钮图像。

我是如何修复的?


我已经在XAML文件中复制了您的代码。所以我的代码是

<Window x:Name="idWindows" x:Class="MemoryGame.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="600" Width="800">
<Window.Resources>
<BitmapImage x:Key="ImageSource" UriSource="Resourcesnao_grigio.png"/>
<BitmapImage x:Key="MouseOverImage" UriSource="Resourcesnao_grigio.png"/>
<BitmapImage x:Key="MousePressed" UriSource="Resourcesnao_grigio.png"/>
<Style x:Key="BtnStyle" TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid x:Name="Gd">
<Grid.Background>
<ImageBrush ImageSource="{StaticResource ResourceKey= ImageSource}" Stretch="Fill"></ImageBrush>
</Grid.Background>
<ContentPresenter></ContentPresenter>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="Button.IsMouseOver" Value="True">
<Setter Property="Background" TargetName="Gd">
<Setter.Value>
<ImageBrush ImageSource="{StaticResource ResourceKey= MouseOverImage}" Stretch="Fill"></ImageBrush>
</Setter.Value>
</Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Button x:Key="MyStyle" Style="{StaticResource BtnStyle}" Height="100" Width="300"/>
</Window.Resources>
<Grid>
<Label Content="Memory" HorizontalAlignment="Left" Margin="320,10,0,0" VerticalAlignment="Top" FontSize="40"/>
<Label x:Name="labelTime" Content="Tempo : " HorizontalAlignment="Left" Margin="628,136,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.211,-0.115"/>
<Label x:Name="labelSecondi" Content="0 sec" HorizontalAlignment="Right" Margin="0,136,10,0" VerticalAlignment="Top"/>
<Label x:Name="labelNumeroErrori" Content="Numero Errori : " HorizontalAlignment="Left" Margin="628,178,0,0" VerticalAlignment="Top"/>
<Label x:Name="labelNumeroErrori_Cont" Content="0" HorizontalAlignment="Right" Margin="0,178,12,0" VerticalAlignment="Top" RenderTransformOrigin="-0.125,-0.692"/>
<Label x:Name="labelNumeroRisposteNonDate" Content="Risposte non date : " HorizontalAlignment="Left" Margin="628,220,0,0" VerticalAlignment="Top"/>
<Label x:Name="labelNumeroRisposteNonDate_Cont" Content="0" HorizontalAlignment="Right" Margin="0,220,12,0" VerticalAlignment="Top" RenderTransformOrigin="-0.125,-0.692"/>
<Grid x:Name="gridToken"  HorizontalAlignment="Left" Height="400" Margin="72,136,0,0" VerticalAlignment="Top" Width="500"/>
</Grid>

在MainWindow.xaml.cs中,我有这个:

Button button = new Button();
ImageBrush brush = new ImageBrush(new BitmapImage(new Uri(Directory.GetCurrentDirectory()+@"Imagesnao_grigio.png")));
brush.Stretch = Stretch.Uniform;
buttonCoppia.Background = brush;
button.Background = brush;
button.Style = this.Resources["MyStyle"] as Style;

但它不起作用。

你必须为你的控件编写一个样式,这样它就可以在其生命周期中保持背景图像(鼠标悬停/退出/点击/聚焦等)。一旦你习惯了,它实际上并不难,所以不必害怕:)

这些链接应该会让你在短时间内覆盖:

如何设置按钮的样式

http://wpftutorial.net/Styles.html

http://msdn.microsoft.com/en-us/library/aa970773%28v=vs.110%29.aspx

试试这个代码

<BitmapImage x:Key="ImageSource" UriSource="DefaultImage.png"/>
<BitmapImage x:Key="MouseOverImage" UriSource="MouseOverImage.png"/>
<BitmapImage x:Key="MousePressed" UriSource="MousePressed.png"/>
<Style x:Key="BtnStyle" TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid x:Name="Gd">
<Grid.Background>
<ImageBrush ImageSource="{StaticResource ResourceKey= ImageSource}" Stretch="Fill"></ImageBrush>
</Grid.Background>
<ContentPresenter></ContentPresenter>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="Button.IsMouseOver" Value="True">
<Setter Property="Background" TargetName="Gd">
<Setter.Value>
<ImageBrush ImageSource="{StaticResource ResourceKey= MouseOverImage}" Stretch="Fill"></ImageBrush>
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="Button.Pressed" Value="False">
<Setter Property="Background" TargetName="Gd">
<Setter.Value>
<ImageBrush ImageSource="{StaticResource ResourceKey= MousePressed}" Stretch="Fill"></ImageBrush>
</Setter.Value>
</Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

<Button Style="{StaticResource BtnStyle}" Height="100" Width="300"/>

相关内容

  • 没有找到相关文章

最新更新