无论如何,有没有动画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(使用前将#替换为@)