views:

108

answers:

2

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.

+2  A: 

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 @ http://blogs.msdn.com/b/expression/archive/2010/02/22/switching-visual-states-easily-using-gotostateaction.aspx

JustinAngel
Legend, il take a look at these tonight!
DeanMc
A: 

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>
Boris Lipschitz
As the OP alludes to this doesn't work in Silverlight.
AnthonyWJones
Yeah, it's a shame but it doesn't work!
DeanMc