deanvmc
deanvmc

Reputation: 6095

How to color textblock on mouse over?

I'm looking to see if there is a way to change the color of a textblock in silverlight on mouse over. I have tried a trigger which I read now doesn't work. I would like to avoid having to do it in the codebehind if at all possible.

Upvotes: 0

Views: 2012

Answers (2)

JustinAngel
JustinAngel

Reputation: 16092

Your instinct on not using code behind for that event is a good one. Allow me to sharpen it though: Don't change the visuals from code-behind, but allow your ViewModels/Code-Behind to own the visual state of the control.

The solution here is to encapsulate the specific visual changes in a custom visual state, and invoke that Visual State either from a ViewModel or a Blend EventTrigger & GoToStateAction.

To learn more about VisualStateManager I strongly recommend you watch these 4 "How Do I" videos by Steve White @ http://expression.microsoft.com/en-us/cc643423.aspx

To learn more about the GoToStateBehavior see @ Link

Upvotes: 2

Boris Lipschitz
Boris Lipschitz

Reputation: 9516

You can set a Style Trigger:

<TextBlock Text="Blah">
    <TextBlock.Style>
        <Style TargetType="{x:Type TextBlock}">
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="Green" />
                    <Setter Property="Foreground" Value="Black" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
 </TextBlock>

Upvotes: -1

Related Questions