You do not need a StackPanel
here, a Rectangle
should be enough to display the line. You can assign a name to your TextBox
. Add a style to your Rectangle
with a DataTrigger
that binds to the TextBox
using its name and checks the IsKeyboardFocusWithin
property to determine if the focus is in the TextBox
. If you still want to use the StackPanel
, you can adapt it easily.
<DockPanel Margin="0,15">
<Rectangle DockPanel.Dock="Bottom"
Width="152"
Margin="0 0 600 0"
Height="1">
<Rectangle.Style>
<Style TargetType="{x:Type Rectangle}">
<Setter Property="Fill" Value="Black"/>
<Style.Triggers>
<DataTrigger Binding="{Binding IsKeyboardFocusWithin, ElementName=MyTextBox}" Value="True">
<Setter Property="Fill" Value="Blue"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Rectangle.Style>
</Rectangle>
<TextBox x:Name="MyTextBox"
Foreground="LightBlue"
Text="Eingabe"
Width="170"
Margin="20 0 0 0"
FontSize="20"
Background="#2D2D30"
BorderThickness="0"
Height="30">
</TextBox>
</DockPanel>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…