开发者

How to color textblock on mouse over?

I'm looking to see if there is a wa开发者_运维技巧y 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.


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


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜