2016-10-19 55 views
0

我已經作爲一個模型this的解決方案,工作正常發光:文本框聚焦時,卻總是暗

<Style x:Key="stlFocusGlowingTextBox" TargetType="{x:Type TextBox}"> 
    <Setter Property="Background" Value="Transparent" /><--------HERE 
     <Setter Property="Effect"> 
       <Setter.Value> 
        <DropShadowEffect ShadowDepth="0" Color="Yellow" Opacity="0" BlurRadius="20"/> 
       </Setter.Value> 
     </Setter> 
     <Style.Triggers> 
      <Trigger Property="IsFocused" Value="True"> 
       <Trigger.EnterActions> 
        <BeginStoryboard> 
         <Storyboard> 
          <DoubleAnimation To="1.0" Storyboard.TargetProperty="(Effect).Opacity" Duration="00:00:00"/> 
         </Storyboard> 
        </BeginStoryboard> 
       </Trigger.EnterActions> 
       <Trigger.ExitActions> 
        <BeginStoryboard> 
         <Storyboard> 
          <DoubleAnimation To="0.0" Storyboard.TargetProperty="(Effect).Opacity" Duration="00:00:02"/> 
         </Storyboard> 
        </BeginStoryboard> 
       </Trigger.ExitActions> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 

唯一的問題是,背景是不是透明的。這就是爲什麼我添加了標記爲<的行----但問題仍然存在,因爲您可以看到上面的文本框應用了樣式併發光但變暗。相反,它應該看起來像下面的一個只適用發光。

enter image description here

預先感謝您的任何幫助 帕特里克

+0

你可以發佈你所有的xaml嗎? – Rom

+2

對於這樣的問題,我強烈建議使用snoop進行調試: http://snoopwpf.codeplex.com/。 Snoop允許您搜索視覺樹,並在運行時修改諸如「背景」之類的屬性。 – GEEF

+0

@Rom我不能這樣做太長,但我可以告訴你,當我應用它時,它只是: Patrick

回答

0

如果你想有一個家長控制的背景下,你可以這樣做:

<Window x:Class="GlowingTextBox.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:local="clr-namespace:GlowingTextBox" 
     mc:Ignorable="d" 
     Title="MainWindow" Height="350" Width="525"> 
    <StackPanel Background="WhiteSmoke"> <!-- The parent with the background color you want for your text box --> 
    <TextBox Width="200" VerticalAlignment="Center" HorizontalAlignment="Center" Name="MyText"> 
     <TextBox.Style> 
     <Style TargetType="{x:Type TextBox}"> 
      <Setter Property="Margin" Value="20" /> 
      <!-- Bind the bacground to the stackpanel --> 
      <Setter Property="Background" Value="{Binding RelativeSource={RelativeSource AncestorType=StackPanel}, Path=Background}" /> 
      <Setter Property="Effect"> 
      <Setter.Value> 
       <DropShadowEffect ShadowDepth="0" Color="Yellow" Opacity="0" BlurRadius="20"/> 
      </Setter.Value> 
      </Setter> 
      <Style.Triggers> 
      <Trigger Property="IsFocused" Value="True"> 
       <Trigger.EnterActions> 
       <BeginStoryboard> 
        <Storyboard> 
        <DoubleAnimation To="1.0" Storyboard.TargetProperty="(Effect).Opacity" Duration="00:00:00"/> 
        </Storyboard> 
       </BeginStoryboard> 
       </Trigger.EnterActions> 
       <Trigger.ExitActions> 
       <BeginStoryboard> 
        <Storyboard> 
        <DoubleAnimation To="0.0" Storyboard.TargetProperty="(Effect).Opacity" Duration="00:00:02"/> 
        </Storyboard> 
       </BeginStoryboard> 
       </Trigger.ExitActions> 
      </Trigger> 
      </Style.Triggers> 
     </Style> 
     </TextBox.Style> 
    </TextBox> 
    <TextBox /> 
    </StackPanel> 
</Window> 

編輯:在上面我已經設置了文本框的背景以引用作爲文本框父級的堆疊面板的背景。看起來,當你使用dropshadoweffect時,如果它的baground設置爲透明,它會通過文本框發光。所以,如果你想在文本框中具有相同的背景,其父(這裏的StackPanel)你引用它作爲<Setter Property="Background" Value="{Binding RelativeSource={RelativeSource AncestorType=StackPanel}, Path=Background}" />

如果你想在文本框有自己的背景,你這樣做:

<Window x:Class="GlowingTextBox.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:local="clr-namespace:GlowingTextBox" 
     mc:Ignorable="d" 
     Title="MainWindow" Height="350" Width="525"> 
    <StackPanel Background="WhiteSmoke"> 
    <TextBox Width="200" VerticalAlignment="Center" HorizontalAlignment="Center" Name="MyText"> 
     <TextBox.Style> 
     <Style TargetType="{x:Type TextBox}"> 
      <Setter Property="Margin" Value="20" /> 
      <Setter Property="Background" Value="White" /> 
      <Setter Property="Effect"> 
      <Setter.Value> 
       <DropShadowEffect ShadowDepth="0" Color="Yellow" Opacity="0" BlurRadius="20"/> 
      </Setter.Value> 
      </Setter> 
      <Style.Triggers> 
      <Trigger Property="IsFocused" Value="True"> 
       <Trigger.EnterActions> 
       <BeginStoryboard> 
        <Storyboard> 
        <DoubleAnimation To="1.0" Storyboard.TargetProperty="(Effect).Opacity" Duration="00:00:00"/> 
        </Storyboard> 
       </BeginStoryboard> 
       </Trigger.EnterActions> 
       <Trigger.ExitActions> 
       <BeginStoryboard> 
        <Storyboard> 
        <DoubleAnimation To="0.0" Storyboard.TargetProperty="(Effect).Opacity" Duration="00:00:02"/> 
        </Storyboard> 
       </BeginStoryboard> 
       </Trigger.ExitActions> 
      </Trigger> 
      </Style.Triggers> 
     </Style> 
     </TextBox.Style> 
    </TextBox> 
    <TextBox /> 
    </StackPanel> 
</Window> 

編輯:在這裏,文本框的背景在樣式中設置爲白色,以使其對基礎顏色無差別。

+0

我可能是愚蠢的,但是...現在我所用的有什麼不同?你只是在本地應用風格,但你正在做我做的... – Patrick

+0

@帕特里克:不完全一樣。請看我上面的編輯。如果他們解決了您的問題,您可以輕鬆嘗試上述xamls。 –