tags:

views:

33

answers:

2

I am creting some border objects on a canvas through code behind. I don't have any XAML for these borders. Their opacity by default is set to 0.5 and I want to change their opacity to 1 on mouse enter. This is the code I have to try and make a mouse enter event for them. But it doesn not work. I think I have to cast the sender object as a border is that correct?

br.MouseEnter += new MouseEventHandler(br_MouseEnter);

    void br_MouseEnter(object sender, MouseEventArgs e)
    {
        sender.Opacity = 1.0;

    }
A: 

Why don't you do?

br.Opacity = 1.0;
jose
+2  A: 

You'll want to do this:-

void br_MouseEnter(object sender, MouseEventArgs e)
{
    ((UIElement)sender).Opacity = 1.0;
}

void br_MouseLeave(object sender, MouseEventArgs e)
{
    ((UIElement)sender).Opacity = 0.5;
}

You could attach these event handlers to multiple Borders.

Although you might also consider creating a templated control and using a VisualStateManager

AnthonyWJones