首页 > 解决方案 > 使用 VisualStateManager 在 MouseOver 上展开 TextBox

问题描述

我在 RecourceDictionary 中定义我自己的 ControlTemplate。我在定义 VisualStateManager 时遇到问题,这是我的代码:

<Style x:Key="SchedulerTextBox" TargetType="TextBox">
    <Setter Property="SnapsToDevicePixels" 
            Value="True" />
    <Setter Property="OverridesDefaultStyle" 
            Value="True" />
    <Setter Property="KeyboardNavigation.TabNavigation"
            Value="None" />
    <Setter Property="AllowDrop"
            Value="True" />
    <Setter Property="Foreground"
            Value="{StaticResource TextForeground}" />
    <Setter Property="Background"
            Value="{StaticResource TextBoxBackground}" />
    <Setter Property="Width"
            Value="50" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="TextBox" >
                <Border Background="{TemplateBinding Background}"
                        BorderBrush="Red"
                        BorderThickness="1"
                        CornerRadius="5"
                        x:Name="Border" 
                        Width="{TemplateBinding Width}"
                        Height="{TemplateBinding Height}">
                    <ScrollViewer Margin="0" x:Name="PART_ContentHost" />

                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates" >
                            <VisualState x:Name="Normal" />
                            <VisualState x:Name="MouseOver">
                                <Storyboard>
                                    <DoubleAnimation Duration="0:0:2"
                                                    By="100" From="{TemplateBinding Width}"
                                                    Storyboard.TargetProperty="Width" />
                                    <DoubleAnimation Duration="0:0:2"
                                                         By="100" From="{TemplateBinding Height}"
                                                         Storyboard.TargetProperty="Height" />
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

我不知道为什么,但文本框显示如下: On Hover

没有鼠标悬停在文本框上看起来像这样: WithoutHover

有谁知道为什么文本框部分显示,我该如何解决这个问题?谢谢你的帮助!

标签: wpfxaml

解决方案


推荐阅读