2017-02-27 3 views
1

は私が私のTextBoxのスタイルを設定するには、この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> 

を再び問題がありますマウスの上にマウスを置いたり、テキストボックスをクリックすると、境界線がデフォルトの青色になります。 しかし、境界厚さになります(醜いですが、デバッグするのには醜いです)。だからこの問題を回避する方法は?

答えて

1

あなたが上にマウスにハードコードされた値にBorderBrushプロパティを設定するデフォルトのテンプレートでトリガがあるのでButtonControlTemplateを上書きする必要があります。

<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'セッターを持っています、それはテンプレートの直前です。 ''これが行です。 何か不足していますか? – agiro