简体   繁体   中英

How do I execute a method on button mouse-over?

I have currently got my buttons setup to change image on mouse-over, however now I'm wondering if it's possible to execute a method on mouse-over. I have been trying to execute just a simple method to display a message box when the mouse is over the button but have had no luck.

This is the code I currently have for my button style and tooltip:

<Style x:Key="btnHoliday" TargetType="{x:Type Button}">
    <Setter Property="IsEnabled" Value="True" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>
                <Image x:Name="PART_img" Source="C:\Users\Development\Documents\Visual Studio 2013\Projects\Program\Resources\Icons\Holiday.png" />
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter TargetName="PART_img"
                    Property="Source"
                    Value="C:\Users\Development\Documents\Visual Studio 2013\Projects\Program\Resources\Icons\HolidayAlt.png" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

and the code for the tooltip

<Button x:Name="btnHoliday" Style="{StaticResource btnHoliday}" 
HorizontalAlignment="Left" Margin="657,19,0,0" VerticalAlignment="Top" Width="64" Height="64" ToolTipService.ShowDuration="12000">
    <Button.ToolTip>
        <StackPanel>
            <TextBlock FontWeight="Bold" FontSize="12" Margin="0,0,0,5">Launch Holidays</TextBlock>
            <TextBlock>
                Allows you to Book/View Holidays, Check
                <LineBreak />
                 the status of pending Bookings and view
                <LineBreak />
                Holidays Remaining.
            </TextBlock>
            <Border BorderBrush="Silver" BorderThickness="0,1,0,0" Margin="0,8" />
            <WrapPanel>
                <TextBlock FontStyle="Italic">Click to launch.</TextBlock>
            </WrapPanel>
        </StackPanel>
    </Button.ToolTip>
</Button>

Before starting to implement anything fancy try to use the MouseEnter/MouseLeave events, and see if it suffice.

Edit :
Add this to the button.

 <i:Interaction.Triggers>
      <i:EventTrigger EventName="MouseEnter">
         <in:CallMethodAction MethodName="Foo" TargetObject="{Binding}" />
      </i:EventTrigger>
  </i:Interaction.Triggers>

import the Blend.Interctivity nuget.

Thanks for your help, However i managed to solve this by creating a method with just a Messagebox to make sure it was running.

private void btnHoliday_MouseEnter(object sender, MouseEventArgs e)
    {
        MessageBox.Show("working");
    }

And then assigned the required event to the method just created using MouseEnter="btnHoliday_MouseEnter" in the XAML code on the Specific Button.

<Button x:Name="btnHoliday" MouseEnter="btnHoliday_MouseEnter" Style="{StaticResource btnHoliday}" HorizontalAlignment="Left" Margin="657,19,0,0" VerticalAlignment="Top" Width="64" Height="64" ToolTipService.ShowDuration="12000">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM