如果你想有一個家長控制的背景下,你可以這樣做:
<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>
編輯:在這裏,文本框的背景在樣式中設置爲白色,以使其對基礎顏色無差別。
你可以發佈你所有的xaml嗎? – Rom
對於這樣的問題,我強烈建議使用snoop進行調試: http://snoopwpf.codeplex.com/。 Snoop允許您搜索視覺樹,並在運行時修改諸如「背景」之類的屬性。 – GEEF
@Rom我不能這樣做太長,但我可以告訴你,當我應用它時,它只是: –
Patrick