wpf - 从一种样式到另一种样式进行动画处理

标签 wpf animation triggers styles

只是想知道是否有人知道如何从一种样式制作动画到另一种样式,即当用户专注于文本框时从NormalStyle转换为ActiveStyle

<Style x:key="NormalStyle" TargetType="{x:Type TextBox}">
    <Setter Property="BorderBrush" Value="Gray" />
    <Setter Property="BorderThickness" Value="2" />
</Style>

<Style x:key="ActiveStyle" TargetType="{x:Type TextBox}" BasedOn="{StaticResource NormalStyle}">
    <Setter Property="BorderBrush" Value="Green" /> 
    <Setter Property="BorderThickness" Value="4" />
</Style>

最佳答案

如果您想交换整个样式,我想您可能必须在代码中完成:

<StackPanel>
    <TextBox Style="{StaticResource NormalStyle}" GotFocus="TextBox_GotFocus" LostFocus="TextBox_LostFocus"/>
    <TextBox Style="{StaticResource NormalStyle}" GotFocus="TextBox_GotFocus" LostFocus="TextBox_LostFocus"/>
</StackPanel>
    private Style focusedStyle;
    private Style normalStyle;

    public MainWindow()
    {
        InitializeComponent();

        focusedStyle = FindResource("ActiveStyle") as Style;
        normalStyle = FindResource("NormalStyle") as Style;
    }

    private void TextBox_GotFocus(object sender, RoutedEventArgs e)
    {
        ((TextBox)sender).Style = focusedStyle;
    }

    private void TextBox_LostFocus(object sender, RoutedEventArgs e)
    {
        ((TextBox)sender).Style = normalStyle;
    }

否则,您几乎只能来回触发。
<TextBox Text="1" >
        <TextBox.Style>
            <Style BasedOn="{StaticResource NormalStyle}" TargetType="{x:Type TextBox}">
                <Style.Triggers>
                    <EventTrigger RoutedEvent="GotFocus">
                        <BeginStoryboard>
                            <Storyboard>
                                <ColorAnimation Storyboard.TargetProperty="BorderBrush.Color" To="Green" Duration="0:0:0.1" />
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>
                    <EventTrigger RoutedEvent="LostFocus">
                        <BeginStoryboard>
                            <Storyboard>
                                <ColorAnimation Storyboard.TargetProperty="BorderBrush.Color" To="Gray" Duration="0:0:0.1" />
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>
                </Style.Triggers>
            </Style>
        </TextBox.Style

关于wpf - 从一种样式到另一种样式进行动画处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1184392/

相关文章:

c# - 组合框 SelectedItem 未按预期工作

javascript - 基于滚动的动画在定义区域之外工作

android - 如何将 View 放置在屏幕外,以便可以通过动画在屏幕上移动?

postgresql - 包含受先前 DELETE 影响的行数的变量? (在函数中)

c# - WPF上下文菜单,复制菜单项变灰

wpf - 动态数据显示X轴平滑滚动

postgresql - 记录 "new"尚未分配

oracle - 如何编写一个基于自动递增值的Oracle触发器?

wpf - 在WPF中删除所有网格的子级

javascript - 如何在JS中写CSS?