2017-02-27 75 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