具有自定义样式的TextBox未激发GotFocus
本文关键字:GotFocus TextBox 自定义 样式 | 更新日期: 2023-09-27 18:20:15
我遇到了一个对一些人来说可能很简单的问题,尽管我尽了最大努力,但我还是无法"解决"这个问题:
我有一个TextBox的自定义样式,在"公共"程序集中定义;此样式覆盖默认样式,因此每次有人使用TextBox时都会使用此样式。问题是,当应用样式时,GotFocus、IsKeyboardFocusWithinChanged和PreviewGotKeyboard福克斯事件不再被激发。我已经验证了只有当应用此样式时才会发生这种情况(如果我注释掉它并运行应用程序,则事件会正确触发)。
所以我的问题是,有人经历过类似的事情吗?如果是的话,有人知道这个问题的解决方案吗?
样式如下(静态资源一个简单的SolidColorBrushes):
<Style TargetType="{x:Type TextBox}" x:Key="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
<Setter Property="OverridesDefaultStyle" Value="True" />
<Setter Property="BorderBrush" Value="{StaticResource BrushBorder}"/>
<Setter Property="ContextMenu" Value="{StaticResource TextBoxContextMenu}" />
<Setter Property="Foreground" Value="{StaticResource BrushForeground}" />
<Setter Property="SelectionBrush" Value="{StaticResource BrushHighlight}" />
<Setter Property="Background" Value="{StaticResource BrushBackground}"/>
<Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
<Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Border BorderThickness="1" x:Name="Border" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}">
<ScrollViewer x:Name="PART_ContentHost"
Margin="0" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsKeyboardFocusWithin" Value="True">
<Setter Property="Background" Value="{StaticResource BrushLightBg}" />
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{StaticResource BrushMouseoverBackground}" />
</Trigger>
</Style.Triggers>
</Style>
嗨,我在这里工作得很好,我不得不添加一些你错过的颜色,这会让你的眼睛流血,对不起,但我有点着急:)
<!-- nice colors you were missing them ;) -->
<SolidColorBrush x:Key="BrushBorder" Color="Blue"/>
<SolidColorBrush x:Key="TextBoxContextMenu" Color="DeepPink"/>
<SolidColorBrush x:Key="BrushForeground" Color="Red"/>
<SolidColorBrush x:Key="BrushHighlight" Color="Aqua"/>
<SolidColorBrush x:Key="BrushBackground" Color="DarkBlue"/>
<SolidColorBrush x:Key="BrushLightBg" Color="LightSeaGreen"/>
<SolidColorBrush x:Key="BrushMouseoverBackground" Color="Yellow"/>
<Style TargetType="{x:Type TextBox}" x:Key="TextBoxStyleMessed" BasedOn="{StaticResource {x:Type TextBox}}" >
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
<Setter Property="OverridesDefaultStyle" Value="True" />
<Setter Property="BorderBrush" Value="{StaticResource BrushBorder}"/>
<!--<Setter Property="ContextMenu" Value="{StaticResource TextBoxContextMenu}" />-->
<Setter Property="Foreground" Value="{StaticResource BrushForeground}" />
<Setter Property="SelectionBrush" Value="{StaticResource BrushHighlight}" />
<Setter Property="Background" Value="{StaticResource BrushBackground}"/>
<Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
<Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Border BorderThickness="1" x:Name="Border" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}">
<ScrollViewer x:Name="PART_ContentHost" Margin="0" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsKeyboardFocusWithin" Value="True">
<Setter Property="Background" Value="{StaticResource BrushLightBg}" />
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{StaticResource BrushMouseoverBackground}" />
</Trigger>
</Style.Triggers>
</Style>
.....
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBox Text="Hello!" HorizontalAlignment="Center" VerticalAlignment="Center" Style="{DynamicResource TextBoxStyleMessed}"
x:Name="HateThisNonMVVMStuff"/>
<Button Grid.Row="1">Hodwy</Button>
</Grid>
不得不偷工减料,但令人讨厌的代码隐藏:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Loaded += MainWindow_Loaded;
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
HateThisNonMVVMStuff.PreviewLostKeyboardFocus += HateThisNonMVVMStuff_PreviewLostKeyboardFocus;
HateThisNonMVVMStuff.LostFocus += UIElement_OnLostFocus;
HateThisNonMVVMStuff.GotFocus += UIElement_OnGotFocus;
}
void HateThisNonMVVMStuff_PreviewLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
System.Diagnostics.Trace.WriteLine("PrevLostFocus!");
}
private void UIElement_OnGotFocus(object sender, RoutedEventArgs e)
{
System.Diagnostics.Trace.WriteLine("Hei! Gained focus");
}
private void UIElement_OnLostFocus(object sender, RoutedEventArgs e)
{
System.Diagnostics.Trace.WriteLine("Hei! Lost focus");
}
}
事件激发了你的风格,我把它当作一种风格只用于这个盒子。我在你的上下文菜单上得到了一个例外,它不会有我讨厌的颜色,但嘿,谁会呢!:D
希望有帮助,
干杯,
Stian