2017-02-27 79 views
1

我有这个Dynamic Resource设置我TextBox的风格:WPF - 文本框样式仍然默认设置时触发火灾

<Style x:Key="TextBoxStyle" TargetType="TextBox"> 
    <Setter Property="FontSize" Value="30"/> 
    <Setter Property="HorizontalContentAlignment" Value="Left"/> 
    <Setter Property="VerticalContentAlignment" Value="Center"/> 
    <Setter Property="HorizontalAlignment" Value="Stretch"/> 
    <Setter Property="VerticalAlignment" Value="Stretch"/> 
    <Setter Property="BorderThickness" Value="2"/> 
    <Setter Property="Background" Value="Transparent"/> 
    <Setter Property="FontFamily" Value="CenturyGothicRegual"/> 
    <Setter Property="Foreground" Value="#FF5B5B5B"/> 
    <Setter Property="Opacity" Value="1"/> 
    <Setter Property="BorderBrush" Value="#FF5B5B5B"/> 
    <!--<Setter Property="FocusVisualStyle"> 
     <Setter.Value> 
      <Style x:Name="ActiveStyle" TargetType="{x:Type TextBox}"> 
       <Setter Property="BorderBrush" Value="Black"/> 
      </Style> 
     </Setter.Value> 
    </Setter>--> 
    <Style.Triggers> 
     <Trigger Property="IsMouseOver" Value="True"> 
      <Setter Property="BorderBrush" Value="Black"/> 
      <Setter Property="BorderThickness" Value="10"/> 
     </Trigger> 
     <Trigger Property="IsFocused" Value="True"> 
      <Setter Property="BorderBrush" Value="Pink" /> 
      <Setter Property="BorderThickness" Value="15"/> 
     </Trigger> 
    </Style.Triggers> 
</Style> 

而且使用它的文本框:

<TextBox Grid.Row="2" Grid.Column="1" Margin="2,2,2,2" Style="{DynamicResource TextBoxStyle}" 
     FontSize="30" Text="asdasd" HorizontalContentAlignment="Left" VerticalContentAlignment="Center" 
     FocusVisualStyle="{StaticResource TextBoxStyle}"> 
</TextBox> 

同样的问题当我将鼠标悬停在鼠标上或单击文本框时,边框将获得默认的蓝色。 然而 borderthickness 确实改变时,我设置它们(丑陋的方式,但需要调试)。那么如何解决这个问题呢?

回答

1

你需要重写ButtonControlTemplate因为在默认模板触发器在BorderBrush属性设置为一个硬编码值鼠标:

<Style x:Key="TextBoxStyle" TargetType="TextBox"> 
    <Setter Property="FontSize" Value="30"/> 
    <Setter Property="HorizontalContentAlignment" Value="Left"/> 
    <Setter Property="VerticalContentAlignment" Value="Center"/> 
    <Setter Property="HorizontalAlignment" Value="Stretch"/> 
    <Setter Property="VerticalAlignment" Value="Stretch"/> 
    <Setter Property="BorderThickness" Value="2"/> 
    <Setter Property="Background" Value="Transparent"/> 
    <Setter Property="FontFamily" Value="CenturyGothicRegual"/> 
    <Setter Property="Foreground" Value="#FF5B5B5B"/> 
    <Setter Property="Opacity" Value="1"/> 
    <Setter Property="BorderBrush" Value="#FF5B5B5B"/> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type TextBox}"> 
       <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True"> 
        <ScrollViewer x:Name="PART_ContentHost" Focusable="false" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"/> 
       </Border> 
       <ControlTemplate.Triggers> 
        <Trigger Property="IsEnabled" Value="false"> 
         <Setter Property="Opacity" TargetName="border" Value="0.56"/> 
        </Trigger> 
        <Trigger Property="IsKeyboardFocused" Value="true"> 
         <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource TextBox.Focus.Border}"/> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
    <Style.Triggers> 
     <Trigger Property="IsMouseOver" Value="True"> 
      <Setter Property="BorderBrush" Value="Black"/> 
      <Setter Property="BorderThickness" Value="10"/> 
     </Trigger> 
     <Trigger Property="IsFocused" Value="True"> 
      <Setter Property="BorderBrush" Value="Pink" /> 
      <Setter Property="BorderThickness" Value="15"/> 
     </Trigger> 
    </Style.Triggers> 
</Style> 
+0

它有点工作,但现在当我点击文本框内部,发生以下异常: **在PresentationFramework.dll中发生类型为“System.InvalidOperationException”的异常,但未在用户代码中处理 附加信息:“{DependencyProperty.UnsetValue}”和“BorderBrush”属性的值无效。 如果有这种异常的处理程序,程序可能会安全地继续。** – agiro

+0

起初我忘了翻译异常抱歉。我现在编辑过,只有部分英文。 – agiro

+0

它有一个'BorderBrush' setter,它就在模板之前。 ''这是线。 我错过了什么吗? – agiro