Transcendent
Transcendent

Reputation: 5755

"MouseOver" in Labels and Buttons in Xaml

I am very new to Xaml but I have been taking advantage of the WPF for a while. For that reason I have made the below method which I use to change the image of a label or button anytime I wanna implement a mouse over or mouse leave event.

public void SImpleImageHadler(dynamic thing, String Path) {
        ImageBrush IB = new ImageBrush();
        IB.ImageSource = new BitmapImage(new Uri("pack://application:,,,/" + Path, UriKind.Absolute));
        thing.Background = IB;
} 

I would like to know how I can do this purely using xaml so that I wouldn't have to make a lot of event handlers for each of my UI elements.

Upvotes: 1

Views: 1563

Answers (2)

DROP TABLE users
DROP TABLE users

Reputation: 1955

In the style triggers for your button you can put this type of code to change properties. To see a image specific example check out this question.

<Style.Triggers>
    <Trigger  Property="IsMouseOver"  Value="True">
         <Setter Property="Background" Value="Blue" />
    </Trigger>
</Style.Triggers>

And from this answer is an image example:

<Trigger Property="IsMouseOver" Value="True">
    <Setter Property="Background">
        <Setter.Value>
            <ImageBrush ImageSource="/MyProjectName;component/Images/MyImage.jpg" />
        </Setter.Value>
    </Setter>
</Trigger>

Upvotes: 3

Erti-Chris Eelmaa
Erti-Chris Eelmaa

Reputation: 26308

You should look into attached properties if you want to encapsulate such stuff for many different types of Buttons/Labels.

The final result should look like this:

<Button
   ns2:MouseOverBackgroundChanger="YourURL"
/>

<Label
  ns2:MouseOverBackgroundChanger="YourUrl2"
/>

This site has everything you need: http://msdn.microsoft.com/en-us/library/ms749011(v=vs.110).aspx (Attached Properties Overview)

Upvotes: 0

Related Questions