在 WPF 中对 TextBox.Foreground 进行动画处理

2024-01-25

无论如何,有没有动画TextBox.ForegroundProperty?

<Color x:Key="NormalColor">#FF666666</Color>
<SolidColorBrush x:Key="NormalBrush" Color="{StaticResource NormalColor}" />

<Color x:Key="MouseOverColor">#FF666666</Color>
<SolidColorBrush x:Key="MouseOverBrush" Color="{StaticResource MouseOverColor}" />

<ControlTemplate x:Key="RegularTextBoxTemplate" TargetType="{x:Type TextBox}">
    <Grid>
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="CommonStates">
                <VisualStateGroup.Transitions>
                    <VisualTransition GeneratedDuration="0:0:0.1"/>
                </VisualStateGroup.Transitions>
                <VisualState x:Name="Normal"/>
                <VisualState x:Name="MouseOver">
                    <Storyboard>
                        <!-- storyboard to animating foreground here... -->
                    </Storyboard>
                </VisualState>
            </VisualStateGroup >
        </VisualStateManager>
        <ScrollViewer x:Name="PART_ContentHost" 
                      BorderThickness="0"
                      IsTabStop="False"
                      Background="{x:Null}"/>
    </Grid>
</ControlTemplate>

<Style x:Key="RegularTextBox" TargetType="{x:Type TextBox}">
    <Setter Property="Foreground" Value="{StaticResource NormalBrush}"/>
    <Setter Property="Template" Value="{StaticResource RegularTextBoxTemplate}"/>
</Style>

我尝试过的故事板是:

<ColorAnimationUsingKeyFrames Storyboard.TargetName="PART_ContentHost"
                  Storyboard.TargetProperty="(Foreground).(SolidColorBrush.Color)">
    <EasingColorKeyFrame KeyTime="0" Value="{StaticResource MouseOverColor}" />
</ColorAnimationUsingKeyFrames>

<ColorAnimationUsingKeyFrames Storyboard.TargetName="PART_ContentHost"
              Storyboard.TargetProperty="(Control.Foreground).(SolidColorBrush.Color)">
    <EasingColorKeyFrame KeyTime="0" Value="{StaticResource MouseOverColor}" />
</ColorAnimationUsingKeyFrames>

<ColorAnimationUsingKeyFrames Storyboard.TargetName="PART_ContentHost"
          Storyboard.TargetProperty="(TextElement.Foreground).(SolidColorBrush.Color)">
    <EasingColorKeyFrame KeyTime="0" Value="{StaticResource MouseOverColor}" />
</ColorAnimationUsingKeyFrames>

<ColorAnimationUsingKeyFrames
                  Storyboard.TargetProperty="(Foreground).(SolidColorBrush.Color)">
    <EasingColorKeyFrame KeyTime="0" Value="{StaticResource MouseOverColor}" />
</ColorAnimationUsingKeyFrames>

<ColorAnimationUsingKeyFrames
              Storyboard.TargetProperty="(TextBox.Foreground).(SolidColorBrush.Color)">
    <EasingColorKeyFrame KeyTime="0" Value="{StaticResource MouseOverColor}" />
</ColorAnimationUsingKeyFrames>

<ColorAnimationUsingKeyFrames
              Storyboard.TargetProperty="(Control.Foreground).(SolidColorBrush.Color)">
    <EasingColorKeyFrame KeyTime="0" Value="{StaticResource MouseOverColor}" />
</ColorAnimationUsingKeyFrames>

<ColorAnimationUsingKeyFrames
          Storyboard.TargetProperty="(TextElement.Foreground).(SolidColorBrush.Color)">
    <EasingColorKeyFrame KeyTime="0" Value="{StaticResource MouseOverColor}" />
</ColorAnimationUsingKeyFrames>

它们都不起作用。任何想法?有可能吗?


好吧,感谢所有试图帮助我的人,我找到了答案。看来当我们设置TextBox.Foreground资源的属性,故事板无法为其设置动画。所以,风格应该是这样的:

<Style x:Key="RegularTextBox" TargetType="{x:Type TextBox}">
    <Setter Property="Foreground">
        <Setter.Value>
            <SolidColorBrush Color="{DynamicResource NormalColor}"/>
        </Setter.Value>
    </Setter>
    <Setter Property="Template" Value="{StaticResource RegularTextBoxTemplate}"/>
</Style>

这是我遇到的唯一问题。但有一个注意事项要记住。当我们想要在情节提要中定位模板化父级时,无需绑定到它。我们只需要留下它:

<!-- It's not necessary to set Storyboard.TargetName in storyboard -->
<!-- It will automatically target the TemplatedParent -->
<ColorAnimationUsingKeyFrames
              Storyboard.TargetProperty="(TextBox.Foreground).(SolidColorBrush.Color)">
    <EasingColorKeyFrame KeyTime="0" Value="{DynamicResource MouseOverColor}" />
</ColorAnimationUsingKeyFrames>

这对我有用。


Here https://github.com/kavand/WpfTextBoxStyle是一个工作示例。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 WPF 中对 TextBox.Foreground 进行动画处理 的相关文章

  • 使用 css 变换在其父对象的宽度上对对象进行动画处理

    我正在尝试使用 css 转换来转换 100 宽度包装器内元素的位置来提高动画性能 因此 它从左侧进入屏幕并从右侧退出 然后重复动画 我想我可以在这个动画中使用百分比 我发现翻译与您正在制作动画的对象相关 因此 如果您有一个 100px 宽的
  • 何时引发 Window.SourceInitialized 事件

    我能保证Window SourceInitialized事件总是会在Window Loaded事件 我需要HwndSource在我的中进一步处理的对象Window Loaded 事件处理程序我不确定到那时这是否总是可用 以下是您可以预期的事
  • WPF:窗口设置边界

    我在 Windows Forms 中使用 SetBounds 方法而不是 Left Top Width Height 属性分配 因为每次我分配更改位置属性的值时 窗口都会更改其位置 Left Top Width Height 赋值导致窗口移
  • 具有自定义镶边的 WPF 窗口在右侧和底部有不需要的轮廓

    我使用 Microsoft Windows Shell dll 创建了带有自定义镶边的 WPF 窗口 这是代码
  • wpf 的 prism 与 mvvm light

    我们正在启动一个带有 MVVM 的 WPF 项目 并且必须决定使用 PRISM 还是 MVVM Light 我对这两个框架都是新手 我已经阅读了一些帖子 但仍然有一些问题 有人可以阐明以下几个方面吗 两个框架 性能 无论出于何种原因 其中一
  • 如何在 WPF 应用程序中实现气球消息

    我们想使用气球消息 如UX Guide http msdn microsoft com en us library aa511451 aspx来自微软 我发现一些示例使用 Windows 窗体中的本机代码 但本机代码需要组件的句柄 这对于
  • Android 中 Activity 之间的 3D 动画

    How to create animation between two Activity look like As Screen shot in android 搜索jazzyviewpager 这是link https github co
  • 在 wpf 中隐藏或禁用输入手势文本

    假设我们使用 Ctrl S 输入手势来保存项目 在 文件 菜单 保存 Ctrl S 中显示这样的文本 现在 在 SaveCommand 的 canexecute 上 我检查项目是否需要保存 如果不需要 则禁用 保存 在这种情况下 我会看到类
  • 如何在 Oxyplot 中显示折线图的绘图点?

    这是我的图表的 xaml 代码
  • 如何(完全)在列表框中实现就地编辑?

    我正在构建一个应用程序 其中ListBox正在显示Description其项目的属性 我想实现与您在 Windows 资源管理器中编辑文件名时发现的相同类型的就地编辑功能 但我发现这需要大量工作 到目前为止我所拥有的是ContextMenu
  • 手动更新目标后,单向绑定停止工作

    我有这样的WPF绑定代码 TestModel source new TestModel TestModel target new TestModel Bind source target BindingMode OneWay source
  • 带有命令绑定的 KeyBinding 不适用于 TextBox UpdateSourceTrigger LostFocus

    我正在使用 MVVM 并遇到以下问题 我的 TextBox Text 与 UpdateSourceTrigger LostFocus 绑定 这就是用户想要的 我有一个带有 SaveCommand CommandBinding 的按钮 这有效
  • 如何将绑定字符串格式设置到所有 DataGridTextColumn 控件?

    这是我的 DataGridTextColumn 控件之一 如下所示
  • 在Linux上编译C# + WPF以便在Windows上运行

    我有一个 C 应用程序 其中某些部分是使用 WPF 编写的 Mono 不支持 可以在 Linux 上编译这个应用程序吗 最终 该应用程序将在 Windows 上运行 但它是更大框架的一部分 并且我们的整个构建过程在 Linux 上运行 因此
  • 如何更改 TextBox.Text 而不丢失 WPF 中的绑定?

    在 WPF 应用程序中 我正在创建一个设置窗口来自定义键盘快捷键 在文本框中 我处理 KeyDown 事件并将 Key 事件转换为人类可读的形式 以及我想要获取数据的形式 文本框声明如下
  • Java 动画中的跳跃和撕裂

    以下代码绘制一个正方形 其中两个较小的正方形在其中旋转 每当你点击键盘上的箭头时 整个系统就会朝那个方向移动 然而 我遇到了一些图像撕裂和有时跳过的问题 它很小但仍然存在 我想知道是否有人知道如何在不大规模更改代码的情况下解决这些问题 im
  • 不支持 URI 前缀

    我正在尝试使用以下方法加载和播放波形文件 SoundPlayer simpleSound new SoundPlayer pack application MyAssembly component Sounds 10meters wav s
  • WPF 中处理系统关闭

    如何在 WPF 中重写 WndProc 当我的窗口关闭时 我尝试检查我正在使用的文件是否被修改 如果是 我必须提示用户 你想保存更改吗 消息 然后关闭正在使用的文件和窗口 但是 当我的窗口仍然打开时 我无法处理用户重新启动 关闭 注销的情况
  • WPF 中列表框的数据验证

    我有一个 ListBox 绑定到类型 T 的 ObservableCollection 每个 ListBoxItem 都是一个复选框 IsChecked 绑定到 T 中的 bool 属性 我想验证 ListBox 中的选中项 以便至少必须选
  • WPF DataGrid - 在每行末尾添加按钮

    我想在数据网格的每一行的末尾添加一个按钮 我找到了以下 xaml 但它将按钮添加到开头 有人知道如何在所有数据绑定列之后添加它吗 这会将按钮添加到开头而不是末尾

随机推荐