【问题标题】:Removing default textbox border animation in xaml删除 xaml 中的默认文本框边框动画
【发布时间】:2014-05-27 09:47:48
【问题描述】:

我的所有文本框都有一个全局样式,如下所示:

<Style TargetType="TextBox">
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="BorderBrush" Value="Red"/>                
        </Trigger>
    </Style.Triggers>
    <Setter Property="BorderBrush" Value="#007acc"/>        
    <Setter Property="Foreground" Value="GhostWhite"/>
    <Setter Property="Background" Value="#49494e"/>
    <Setter Property="HorizontalAlignment" Value="Left"/>
    <Setter Property="Width" Value="250"/>
    <Setter Property="Margin" Value="0,0,0,5"/>
</Style>

当我用鼠标将鼠标悬停在文本框上时,它的边框颜色会在几秒钟内正确捕捉到红色,但随后默认动画会接管并将其更改为默认的文本框蓝色边框颜色。

如何在不重新定义整个文本框模板的情况下禁用或覆盖此动画?我只希望边框颜色保持红色,而鼠标光标悬停在文本框上。

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:

    解决您的问题的一个方法是更改​​ Template of TextBox

    <Style TargetType="{x:Type TextBox}">
       <Style.Triggers>
          <Trigger Property="IsMouseOver" Value="True">
             <Setter Property="BorderBrush" Value="Red"/>
          </Trigger>
       </Style.Triggers>
       <Setter Property="Template">
          <Setter.Value>
             <ControlTemplate TargetType="{x:Type TextBox}">
                <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1">
                   <ScrollViewer x:Name="PART_ContentHost"/>
                </Border>
             </ControlTemplate>
          </Setter.Value>
       </Setter>
       <Setter Property="BorderBrush" Value="#007acc"/>
       <Setter Property="Foreground" Value="GhostWhite"/>
       <Setter Property="Background" Value="#49494e"/>
       <Setter Property="HorizontalAlignment" Value="Left"/>
       <Setter Property="Width" Value="250"/>
       <Setter Property="Margin" Value="0,0,0,5"/>
    </Style>
    

    【讨论】:

      【解决方案2】:

      这实际上是属性优先级的一个已知问题。基本上,您的边框画笔的本地属性设置器与您的触发器发生冲突。这个Msdn article 更全面地解释了它,但基本上如果你删除

          <Setter Property="BorderBrush" Value="#007acc"/>  
      

      您的触发器应该可以正常工作

      【讨论】:

      • 如果我删除它,那么当它没有被悬停时我将失去我的边框颜色,如果我尝试通过将设置器移动到 那么它会完全按照动画之前所做的那样做。
      猜你喜欢
      • 2021-06-20
      • 1970-01-01
      • 1970-01-01
      • 2015-07-08
      • 1970-01-01
      • 1970-01-01
      • 2021-08-10
      • 2014-07-14
      • 1970-01-01
      相关资源
      最近更新 更多