TextBox在聚焦时发光,但总是很暗

时间:2016-10-19 13:00:08

标签: c# wpf background textbox styles

我采用了正常的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>

唯一的问题是背景不透明。这就是为什么我添加标有&lt; ----的行,但问题仍然存在,因为您可以看到上面的文本框已应用样式并发光但变暗。相反,它应该看起来像下面只有应用发光的那个。

enter image description here

提前感谢您的帮助 帕特里克

1 个答案:

答案 0 :(得分: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>

编辑:在上面我设置了文本框的背景,以引用作为文本框父级的stackpanel的背景。似乎当你使用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>

编辑:此处文本框的背景在样式中设置为白色,以使其对底层颜色无动于衷。

相关问题