在我的下面的代码中,我为最顶层的TextBox创建了一个触发器和一个故事板。当鼠标进入最顶层的TextBox时,它变为红色,当鼠标进入其余部分时,它们不会。 我的问题是如何修改我提到的触发器和故事板中的任何一个或两个,这样即使鼠标进入其余的TextBox,它们也会像最顶层的那样变成红色。 在我的真实应用中,我有数百个文本框,我不想为每个单独创建提到的触发器
<Window x:Class="WpfApplication8.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:WpfApplication8"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Storyboard x:Key="OnMouseEnter1">
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="textBox">
<EasingColorKeyFrame KeyTime="0" Value="White"/>
<EasingColorKeyFrame KeyTime="0:0:1" Value="Red"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</Window.Resources>
<Window.Triggers>
<EventTrigger RoutedEvent="UIElement.MouseEnter" SourceName="textBox">
<BeginStoryboard Storyboard="{StaticResource OnMouseEnter1}"/>
</EventTrigger>
</Window.Triggers>
<Grid>
<TextBox x:Name="textBox" Height="23" Margin="173.561,97.634,223.439,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top"/>
<TextBox x:Name="textBox1" Height="23" Margin="173.561,125.634,223.439,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top"/>
<TextBox x:Name="textBox2" Margin="173.561,153.634,223.439,141.463" TextWrapping="Wrap" Text="TextBox"/>
</Grid>
答案 0 :(得分:1)
您需要重新模板化TextBoxBase样式。
<Window.Resources>
<Style TargetType="{x:Type TextBoxBase}">
<Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="MinWidth" Value="120"/>
<Setter Property="MinHeight" Value="20"/>
<Setter Property="AllowDrop" Value="true"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBoxBase}">
<Border
Name="Border"
CornerRadius="2"
Padding="2"
Background="White"
BorderBrush="Black"
BorderThickness="1" >
<ScrollViewer Margin="0" x:Name="PART_ContentHost" BorderThickness="0" Background="Transparent"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="Border" Property="Background" Value="LightGray"/>
<Setter TargetName="Border" Property="BorderBrush" Value="Gray"/>
<Setter Property="Foreground" Value="WhiteSmoke"/>
</Trigger>
<EventTrigger RoutedEvent="UIElement.MouseEnter">
<BeginStoryboard>
<Storyboard>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)" Storyboard.TargetName="Border">
<EasingColorKeyFrame KeyTime="0" Value="White"/>
<EasingColorKeyFrame KeyTime="0:0:1" Value="Red"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<TextBox x:Name="textBox" Height="23" Margin="173.561,97.634,223.439,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top"/>
<TextBox x:Name="textBox1" Height="23" Margin="173.561,125.634,223.439,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top"/>
<TextBox x:Name="textBox2" Margin="173.561,153.634,223.439,141.463" TextWrapping="Wrap" Text="TextBox"/>
</Grid>