Any way to simulate MouseOver in WPF

Posted by jpierson on Stack Overflow See other posts from Stack Overflow or by jpierson
Published on 2010-05-25T19:03:53Z Indexed on 2010/05/25 19:21 UTC
Read the original article Hit count: 500

Filed under:
|
|

I'm working on a link control in WPF which fits the text with icon links case in the Windows UX Guide. What I want is to have some text within a hyperlink that appears to the right of some image.

In my case I started off by using a TextBlock that contained a Hyperlink which then contained my image and some text.

<TextBlock>
        <Hyperlink>
            <Rectangle Height="16"
                       Width="16"
                       Fill="{StaticResource MyIconBrush}"
                       Stretch="UniformToFill"
                       VerticalAlignment="Center"
                       HorizontalAlignment="Left" />
            <Run>My link text</Run>
        </Hyperlink>
</TextBlock>

The problem with this however was that the image being taller than my text produced an effect where the text was aligned to the bottom. Unfortunately I haven't found any way to control the vertical alignment within the TextBlock or within the Hyperlink so I've resorted to attempting an alternative layout where the Hyperlink and the Rectangle that represent my vector icon are separated in order to get them to align properly like shown below.

<TextBlock>
        <Hyperlink>
            <StackPanel Orientation="Horizontal">
                <Rectangle Height="16"
                           Width="16"
                           Fill="{StaticResource MyIconBrush}"
                           Stretch="UniformToFill"
                           VerticalAlignment="Center"
                           HorizontalAlignment="Left" />
                <TextBlock VerticalAlignment="Center"><Hyperlink>My link text<Hyperlink></TextBlock>
            </StackPanel>
        </Hyperlink>
</TextBlock>

The problem with this however is that now that my Icon and my Hyperlink are separated I don't get my MouseOver appearance of my link when I the mouse is over my icon and vise-versa.

So this got me to thinking, how do I simulate MouseOver for a given control such with a checkbox where you get the MouseOver effect on the box when you actually mouse over it's associated text. I know in the HTML world the label element has a for attribute that can be used to specify which control it is labeling which will basically do what I'm looking for. Also I can imagine that in other scenarios it may be nice to have a label that when you mouse over shows a corresponding text box as if the mouse is over it and possibly when clicked focus is given to the corresponding text box as well. For now though I'm interested mainly in how to to get a label or label like element in WPF to act as a proxy for a given control in terms of it's MouseOver state. Also I would like to do this purely in XAML if possible.

© Stack Overflow or respective owner

Related posts about wpf

Related posts about mouseover