I've got a Border control, which holds an Image control. Basicly i want to show the Border control if the Image control is hovered. Something similar to the css :hover effect.
What's the easiest way achieve this?
There are so many ways to achieve this, and it all depends on what your setup is.
DataTemplate
for a ListView
or some other items control?If it's for just one image, you could handle the PointerEntered
and PointerExited
events on the image control to set the border's BorderBrush
color to yellow (for example) and transparent, respectively. Since you said you're very new to XAML, this is the only "easiest" solution that I can offer. Maybe someone else can suggest other simple alternatives. Unfortunately, there is no ":hover"-like styling out of the box. You have to write this functionality yourself.
My preferred solution is to bundle this functionality up into some kind of reusable component. Some solutions that come to mind:
UserControl
housing a Border
and Image
with the necessary code-behind to handle pointer enter and exit events.ContentControl
, allowing you to set any arbitrary UIElement
inside the Border
. This also allows you to use TemplateBinding
s to set things like the BorderBrush
from outside the template.Behavior
(Blend SDK) that can be attached to your Border
that houses the necessary logic.On the topic of the Blend SDK, solutions 1 and 2 can be achieved with zero code-behind if you make use of VisualState
s and blend's EventTriggerBehavior
and GoToStateAction
to handle changing visual states on PointerEntered
and PointerExited
events.